How to remove newline characters from string in php

王林
Release: 2023-03-04 16:34:01
Original
3613 people have browsed it

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

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

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!

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