如何从PHP字符串中删除新行和返回? [英] how to remove new lines and returns from php string?
本文介绍了如何从PHP字符串中删除新行和返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个php变量包含以下字符串:
A php variable contains the following string:
<p>text</p>
<p>text2</p>
<ul>
<li>item1</li>
<li>item2</li>
</ul>
我想删除此字符串中的所有换行符,以便字符串看起来像这样:
I want to remove all the new line characters in this string so the string will look like this:
<p>text</p><p>text2><ul><li>item1</li><li>item2</li></ul>
我尝试了以下操作但未成功:
I've tried the following without success:
str_replace('\n', '', $str);
str_replace('\r', '', $str);
str_replace('\r\n\', '', $str);
有人知道如何解决这个问题吗?
Anyone knows how to fix this?
推荐答案
您需要将\n
用双引号引起来.
在单引号中将其视为2个字符,然后按'n'
You need to place the \n
in double quotes.
Inside single quotes it is treated as 2 characters '\'
followed by 'n'
您需要:
$str = str_replace("\n", '', $str);
更好的替代方法是将PHP_EOL
用作:
A better alternative is to use PHP_EOL
as:
$str = str_replace(PHP_EOL, '', $str);
这篇关于如何从PHP字符串中删除新行和返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文