Home  >  Article  >  Backend Development  >  How to remove newline characters from string in php

How to remove newline characters from string in php

王林
王林Original
2020-08-17 15:26:123576browse

php method to remove newlines in strings: You can use the php system constant PHP_EOL combined with the str_replace() function to achieve this. Line breaks in PHP can be replaced with PHP_EOL. The str_replace() function is used to replace some characters in a string.

How to remove newline characters from string in php

str_replace() function replaces some characters in a string (case-sensitive) and returns a string or array with the replacement value.

(Recommended tutorial: php graphic tutorial)

Grammar:

str_replace(find,replace,string,count)

(Recommended learning video: php video tutorial )

Implementation code:

// 1)使用转义字符函数
<?php
$str = str_replace(array("/r/n", "/r", "/n"), &#39;&#39;, $str);
?>
// 2)使用正则表达式替换
<?php
$str = preg_replace(&#39;//s*/&#39;, &#39;&#39;, $str);
?>
// 3)使用PHP系统常量【推荐】
$str = str_replace(PHP_EOL, &#39;&#39;, $str);
?>

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