Home  >  Article  >  Backend Development  >  How to replace newlines in strings in php

How to replace newlines in strings in php

王林
王林Original
2020-10-12 10:10:053516browse

php method to replace newline characters in strings: You can use the variable PHP_EOL defined in php to replace, such as [str_replace(PHP_EOL, '', $str);].

How to replace newlines in strings in php

You can use the following three methods to solve it:

(Recommended tutorial: php video tutorial)

1. Use str_replace to replace newlines

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

2. Use regular replacement

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

3. Use variables defined by php (recommended)

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

Related recommendations : php training

The above is the detailed content of How to replace newlines in strings 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