Home >Backend Development >PHP Problem >How to replace carriage return and line feed in php

How to replace carriage return and line feed in php

藏色散人
藏色散人Original
2020-07-09 13:03:273521browse

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);

The second way is to use regular expressions

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

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);

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!

Statement:
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