Home > Backend Development > PHP Problem > How to replace newline characters in php

How to replace newline characters in php

青灯夜游
Release: 2023-03-09 15:06:02
Original
4249 people have browsed it

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";
Copy after login

Method 1:

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

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

Method 2 :

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

(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);
Copy after login

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!

Related labels:
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