【问题标题】:how can i remove "\n" in string in php? [duplicate]如何在 php 中删除字符串中的“\n”? [复制]
【发布时间】:2013-05-04 20:00:08
【问题描述】:

如何去掉字符串中的“\n”?
我使用了下面的 3 种方法,只有 "\s+" 有效的接缝,但我只想删除 \n,而不是所有空间,如何完成这项工作?
需要帮助,这个问题困扰了我很久。

$str="<p>
    hello<br>
    world
</p>";

//$str=str_replace("\n","",$str);
$str=preg_replace("@\n@","",$str);
//$str=preg_replace("@\s+@"," ",$str);  
echo $str;

【问题讨论】:

标签: php


【解决方案1】:

这也应该可以解决问题。

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

【讨论】:

  • 好,这就是我想要的,谢谢
  • 这是正确答案
  • 注意如果是 linux/unix 系统应该是 str_replace("\n","",$str);
  • $str=str_replace("\r","",str_replace("\n","",$str));
【解决方案2】:
$string = trim(preg_replace('/\s\s+/', ' ', $string));

上面的代码将用一个空格替换多个空格和换行符。

【讨论】:

  • 当我试图在 HTML 字符串中删除新行时,这个对我有用。谢谢!
  • 漂亮!非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 2011-05-10
相关资源
最近更新 更多