Home  >  Article  >  Backend Development  >  How to replace the carriage return character in php

How to replace the carriage return character in php

藏色散人
藏色散人Original
2021-03-12 17:58:282699browse

How to replace the carriage return character in php: 1. Replace through the str_replace method; 2. Replace using the preg_replace method; 3. Replace through the "str_replace(PHP_EOL, '', $str);" statement. Can.

How to replace the carriage return character in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Three ways to replace carriage return and line feed with PHP Method

A small line break actually has different implementations on different platforms. Why is this? The world is diverse!

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

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

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

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. Under Windows, it will be /r/ n, under Linux it is /n, under mac it is /r

$str = str_replace(PHP_EOL, '', $str);

[Recommended learning: "PHP Video Tutorial》】

The above is the detailed content of How to replace the carriage return character 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