PHP为什么输出 而不是实际换行?

问题描述 投票:0回答:1

为什么 PHP 在这里输出

\n
而不是真正换行?我正在终端中查看它并保存到文本文件中。

显示错误:

Lopende tekst...\nVolgende zin.

它应该导致:

Lopende tekst...
Volgende zin.

代码:

<?php
$txt="Lopende tekst... Volgende zin.";
$txt=preg_replace('/(\.{3})( )([A-Z])/','\\1\n\\3',$txt);
echo($txt);
?>
php newline
1个回答
2
投票

对于基本字符串,单引号

''
没有像转换后的
\n
这样的特殊字符,如果需要,可以使用双引号
""
。正则表达式是例外,因为它有额外的处理,其中考虑到了这一点。但替换字符串只有捕获组。

<?php
$txt="Lopende tekst... Volgende zin.";
$txt=preg_replace('/(\.{3})( )([A-Z])/',"\\1\n\\3",$txt);
echo($txt);
?>
© www.soinside.com 2019 - 2024. All rights reserved.