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

How to replace the carriage return character in php

藏色散人
Release: 2023-03-08 17:38:01
Original
2732 people have browsed it

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

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

[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!

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