【问题标题】:Regexp newline in replacements替换中的正则表达式换行符
【发布时间】:2014-03-10 16:46:21
【问题描述】:

我需要用换行符替换所有空格,但是当我写 \n 作为替换时,它只是变成了 \n 作为文本。

我该如何解决?我花了很多时间在互联网上寻找这个,但我没有成功。

【问题讨论】:

  • 你在什么环境下执行正则表达式?
  • 嘿 :) 它是 php preg_replace

标签: php regex replace newline


【解决方案1】:

这对我有用:

$str = 'this is my string';
$str = preg_replace('/\s/', "\n", $str);
var_dump($str);

\n 需要用双引号括起来,以便插入为换行符。

此外,如果您对 HTML 输出执行此操作,您将看不到任何差异。您需要将空格替换为 <br/>:

$str = preg_replace('/\s/', '<br/>', $str);

【讨论】:

    猜你喜欢
    • 2013-01-10
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    相关资源
    最近更新 更多