PHP preg_replace 只有在特定子字符串之后才有更多空格
PHP preg_replace more spaces only when after specific substring
我想确保包含代码片段的多行字符串具有足够的可读性。目标是用不间断的空格替换正常的空格,以便代码片段在网络浏览器中可读。
我已经用 nl2br 替换了所有 cr+lf。
但是我需要用 HTML break 标记替换所有空格
不应触及其他空格。
我试过了:
$text = nl2br($text);
$text = preg_replace('/(<br \/>)+(\s+)/', ' ', $text);
但这只替换了 HTML 中断标记后的一个空格。
我想要文本(nl2br 的结果):
some text<br /> some other text
成为:
some text<br /> some other text
如何正确使用 preg_replace?
使用preg_replace_callback
:
$text = preg_replace_callback('#<br\s*/?>(\s+)#', function ($match) {
$pad = '';
$l = strlen($match[1]);
while ($l-- > 0)
$pad .= ' ';
return '<br />' . $pad;
}, $text);`
并且只是包裹在 <pre>
标签中:
$text = "normal text\nother normal text\n some code snippet\n some code snippet\njust another normal text\njust another normal text\n some other code snippet";
$text2 = str_replace("\r", '', $text);
$text2 = preg_replace('#((\n\s+[^\n$]+)+)(\n|$)#', '<pre>[]</pre>', $text2);
$text2 = str_replace("\n", '<br />', $text2);
echo "<div>$text2</div>";
结果:
<div>normal text<br />other normal text<pre>[<br /> some code snippet<br /> some code snippet]</pre>just another normal text<br />just another normal text<pre>[<br /> some other code snippet]</pre></div>
...显示为:
normal text
other normal text
[
some code snippet
some code snippet]
just another normal text
just another normal text
[
some other code snippet]
注意 "code snippets".
两边的方括号
正确的方法是:
$c_text = str_replace("\r", '', $c_text);
$c_text = str_replace("\n", '<br />', $c_text);
$c_text = preg_replace_callback('#<br \/>(\s+)#', function ($match) {
return '<br />' . str_pad("", strlen($match[1]) * 6, " ");
}, $c_text);
这使文本显示为我想要的。
上面带有 pre
标签的解决方案将不起作用,因为即使没有前导空格,代码片段也可能从换行符开始。
另请记住,nl2br 不会替换 cr+lf,因此需要 str_replace,如本答案和上一个答案所示。
最终结果可在http://tkweb.eu/en/delphicomp/kcontrols.html查看,文中为文章下方的评论。这个网站是很久以前建立的,现在我只需要快速提高评论的可读性就可以更改它们。因为我现在没时间做更好的系统
我想确保包含代码片段的多行字符串具有足够的可读性。目标是用不间断的空格替换正常的空格,以便代码片段在网络浏览器中可读。
我已经用 nl2br 替换了所有 cr+lf。 但是我需要用 HTML break 标记替换所有空格 不应触及其他空格。
我试过了:
$text = nl2br($text);
$text = preg_replace('/(<br \/>)+(\s+)/', ' ', $text);
但这只替换了 HTML 中断标记后的一个空格。
我想要文本(nl2br 的结果):
some text<br /> some other text
成为:
some text<br /> some other text
如何正确使用 preg_replace?
使用preg_replace_callback
:
$text = preg_replace_callback('#<br\s*/?>(\s+)#', function ($match) {
$pad = '';
$l = strlen($match[1]);
while ($l-- > 0)
$pad .= ' ';
return '<br />' . $pad;
}, $text);`
并且只是包裹在 <pre>
标签中:
$text = "normal text\nother normal text\n some code snippet\n some code snippet\njust another normal text\njust another normal text\n some other code snippet";
$text2 = str_replace("\r", '', $text);
$text2 = preg_replace('#((\n\s+[^\n$]+)+)(\n|$)#', '<pre>[]</pre>', $text2);
$text2 = str_replace("\n", '<br />', $text2);
echo "<div>$text2</div>";
结果:
<div>normal text<br />other normal text<pre>[<br /> some code snippet<br /> some code snippet]</pre>just another normal text<br />just another normal text<pre>[<br /> some other code snippet]</pre></div>
...显示为:
normal text
other normal text
[
some code snippet
some code snippet]
just another normal text
just another normal text
[
some other code snippet]
注意 "code snippets".
两边的方括号正确的方法是:
$c_text = str_replace("\r", '', $c_text);
$c_text = str_replace("\n", '<br />', $c_text);
$c_text = preg_replace_callback('#<br \/>(\s+)#', function ($match) {
return '<br />' . str_pad("", strlen($match[1]) * 6, " ");
}, $c_text);
这使文本显示为我想要的。
上面带有 pre
标签的解决方案将不起作用,因为即使没有前导空格,代码片段也可能从换行符开始。
另请记住,nl2br 不会替换 cr+lf,因此需要 str_replace,如本答案和上一个答案所示。
最终结果可在http://tkweb.eu/en/delphicomp/kcontrols.html查看,文中为文章下方的评论。这个网站是很久以前建立的,现在我只需要快速提高评论的可读性就可以更改它们。因为我现在没时间做更好的系统