Home  >  Article  >  Backend Development  >  How to replace newline characters in php

How to replace newline characters in php

青灯夜游
青灯夜游Original
2021-05-10 14:06:184148browse

Method: 1. Use the "str_replace(array("\r\n","\n"),'', variable)" statement; 2. Use "preg_replace('/[\r\n ]/','', variable)" statement; 3. Use the "str_replace(PHP_EOL,'', variable)" statement.

How to replace newline characters in php

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

How to replace line breaks in php:

$str="this is a test \n";

Method 1:

$replace_str = str_replace(array("\r\n", "\r", "\n"), "", $str);

(Note: replace \r\n first, then whether it exists\n, and finally replace \r)

Method 2 :

$replace_str = preg_replace('/[\r\n]/', '', $str);

(The original text is /\s*/, but this will also replace the blank characters in the content)

Method 3:

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

Recommended learning : "PHP Video Tutorial"

The above is the detailed content of How to replace newline characters 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