php 如何用新行替换所有 XHTML/HTML 换行符 (<br>)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2436145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:28:24  来源:igfitidea点击:

How to replace all XHTML/HTML line breaks (<br>) with new lines?

phpregexnewline

提问by markb

I am looking for the best br2nlfunction. I would like to replace all instances of <br>and <br />with newlines \n. Much like the nl2br()function but the opposite.

我正在寻找最好的br2nl功能。我想更换的所有实例<br>,并<br />用换行\n。很像nl2br()函数,但相反。

I know there are several solutions in the PHP manual comments but I'm looking for feedback from the SO community on possible solutions.

我知道 PHP 手册注释中有几种解决方案,但我正在寻找来自 SO 社区的关于可能解决方案的反馈。

回答by Pascal MARTIN

I would generally say "don't use regex to work with HTML", but, on this one, I would probably go with a regex, considering that <br>tags generally look like either :

我通常会说“不要使用正则表达式来处理 HTML”,但是,在这一点上,我可能会使用正则表达式,考虑到<br>标签通常看起来像:

  • <br>
  • or <br/>, with any number of spaces before the /
  • <br>
  • <br/>, 前面有任意数量的空格/


I suppose something like this would do the trick :


我想这样的事情可以解决问题:

$html = 'this <br>is<br/>some<br />text <br    />!';
$nl = preg_replace('#<br\s*/?>#i', "\n", $html);
echo $nl;

Couple of notes :

几个注意事项:

  • starts with <br
  • followed by any number of white characters : \s*
  • optionnaly, a /: /?
  • and, finally, a >
  • and this using a case-insensitive match (#i), as <BR>would be valid in HTML
  • 以。。开始 <br
  • 后跟任意数量的白色字符: \s*
  • 可选地,一个//?
  • 最后,一个 >
  • 并且使用不区分大小写的匹配 ( #i),<BR>这在 HTML 中是有效的

回答by Antti

You should be using PHP_EOLconstant to have platform independent newlines.

您应该使用PHP_EOL常量来获得独立于平台的换行符。

In my opinion, using non-regexp functions whenever possible makes the code more readable.

在我看来,尽可能使用非正则表达式函数会使代码更具可读性。

$newlineTags = array(
  '<br>',
  '<br/>',
  '<br />',
);
$html = str_replace($newlineTags, PHP_EOL, $html);

I am aware this solution has some flaws, but wanted to share my insights still.

我知道这个解决方案有一些缺陷,但仍然想分享我的见解。

回答by VolkerK

If the document is well-formed (or at least well-formed-ish) you can use the DOM extensionand xpath to find and replace all br elements by a \n text node.

如果文档格式良好(或至少格式良好),您可以使用DOM 扩展和 xpath 查找所有 br 元素并将其替换为 \n 文本节点。

$in = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>...</title></head><body>abc<br />def<p>ghi<br />jkl</p></body></html>';

$doc = new DOMDOcument;
$doc->loadhtml($in);
$xpath = new DOMXPath($doc);

$toBeReplaced = array();
foreach($xpath->query('//br') as $node) {
    $toBeReplaced[] = $node;
}

$linebreak = $doc->createTextNode("\n");
foreach($toBeReplaced as $node) {
    $node->parentNode->replaceChild($linebreak->cloneNode(), $node);
}

echo $doc->savehtml();

prints

印刷

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>...</title></head>
<body>abc
def<p>ghi
jkl</p>
</body>
</html>

edit: shorter version with only one iteration

编辑:只有一次迭代的较短版本

$in = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>...</title></head><body>abc<br />def<p>ghi<br />jkl</p></body></html>';

$doc = new DOMDOcument;
$doc->loadhtml($in);
$xpath = new DOMXPath($doc);

$linebreak = $doc->createTextNode("\n");
foreach($xpath->query('//br') as $node) {
  $node->parentNode->removeChild($node);
}

echo $doc->savehtml();

回答by ssergei

From the nl2brcomments:

nl2br评论:

<?php
function br2nl($string){
  $return=eregi_replace('<br[[:space:]]*/?'.
    '[[:space:]]*>',chr(13).chr(10),$string);
  return $return;
}
?>