How to replace carriage return and line feed in php

藏色散人
Release: 2023-03-02 08:26:02
Original
3423 people have browsed it

How to replace carriage returns in php: 1. Use the statement "str_replace(array("/r", "/n", "/r/n"), "", $string);"; 2 , using regular expressions; 3. Through the statement "$str = str_replace(PHP_EOL,)".

How to replace carriage return and line feed in php

Three ways to replace carriage return and line feed in PHP

A small line break actually works on different platforms There are different implementations, why is this? The world is diverse!

Originally, /n is used to replace line breaks in the Unix world.

In order to reflect the difference, Windows uses /r/n.

What’s more interesting is that in Mac Used /r.

Therefore, there are a lot of troubles when writing programs on different platforms, such as replacing newlines.

The first way

str_replace(array("/r", "/n", "/r/n"), "", $string);
Copy after login

The second way is to use regular expressions

$str = preg_replace('//s*/', '', $str);
Copy after login

The third way

I have to look at it again here One of PHP's defined variables

PHP_EOL represents the newline character of PHP. This variable will change according to the platform. It will be /r/n under Windows and /n under Linux. Under mac it is /r

$str = str_replace(PHP_EOL, '', $str);
Copy after login

For more related articles, please visit PHP Chinese website!

The above is the detailed content of How to replace carriage return and line feed in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!