[英]How to remove redundant <br /> tags from HTML code using PHP?
I'm parsing some messy HTML code with PHP in which there are some redundant我正在用 PHP 解析一些凌乱的 HTML 代码,其中有一些冗余
tags and I would like to clean them up a bit.标签,我想清理一下。 For instance:
例如:
<br>
<br /><br />
<br>
How would I replace something like that with this using preg_replace()?:我如何使用 preg_replace() 替换类似的东西?:
<br /><br />
Newlines, spaces, and the differences between <br>
, <br/>
, and <br />
would all have to be accounted for.换行符、空格以及
<br>
、 <br/>
和<br />
之间的差异都必须考虑在内。
Edit: Basically I'd like to replace every instance of three or more successive breaks with just two.编辑:基本上我想用两个替换三个或更多连续中断的每个实例。
Here is something you can use.这是您可以使用的东西。 The first line finds whenever there is 2 or more
<br>
tags (with whitespace between and different types) and replace them with wellformated <br /><br />
.只要有 2 个或更多
<br>
标签(在不同类型之间有空格),第一行就会找到,并将它们替换为格式正确的<br /><br />
。
I also included the second line to clean up the rest of the <br>
tags if you want that too.如果你也想要的话,我还包括了第二行来清理
<br>
标签的 rest。
function clean($txt)
{
$txt=preg_replace("{(<br[\\s]*(>|\/>)\s*){2,}}i", "<br /><br />", $txt);
$txt=preg_replace("{(<br[\\s]*(>|\/>)\s*)}i", "<br />", $txt);
return $txt;
}
This should work, using minimum specifier:这应该工作,使用最小说明符:
preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br /><br />', $multibreaks);
Should match appalling <br><br /><br/><br>
constructions too.也应该匹配骇人听闻的
<br><br /><br/><br>
结构。
this will replace all breaks... even if they're in uppercase:这将替换所有中断......即使它们是大写的:
preg_replace('/<br[^>]*>/i', '', $string);
Try with:尝试:
preg_replace('/<br\s*\/?>/', '', $inputString);
Use str_replace, its much better for simple replacement, and you can also pass an array instead of a single search value.使用 str_replace,它更适合简单的替换,您还可以传递一个数组而不是单个搜索值。
$newcode = str_replace("<br>", "", $messycode);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.