将两个 BR 标签之间的所有内容替换为 PHP
Replace everything between two BR tags with PHP
如何替换介于以下之间的所有内容:
<br />
<b>
和
<br />
例如:
<br />
<b>Notice</b>: Undefined variable: XXX in <b>YYY</b> on line <b>ZZZ<br />
<b>
注意:我知道我可以关闭错误报告。但在这种情况下,我需要用一些现有的 HTML 代码替换它们。
$string_to_replace = '<div>
<p>Some content</p>
<br />
<b>Notice</b>: Undefined variable: XXX in <b>YYY</b> on line <b>ZZZ</b><br />
<p>Some other content</p>
</div>';
$string_without_warnings = preg_replace('<br \/>(.*?)<br \/>', '', $string_to_replace);
使用正则表达式解析 html 不是个好主意。请参阅这个著名的 SO post 以了解原因。 RegEx match open tags except XHTML self-contained tags
话虽如此,您的要求当然是可能的,但我应该指出,根据您传入的内容,它可能表现得 differently/buggy,因此我们不鼓励这样做。
首先你的正则表达式:https://regex101.com/r/5fvuyi/1
<br \/>\n?(?<replace>.*)<br \/>
我使用了命名捕获组,您可以看到我在代码中引用了它。 https://3v4l.org/uSVYZ
<?php
$string_to_replace = '<div>
<p>Some content</p>
<br />
<b>Notice</b>: Undefined variable: XXX in <b>YYY</b> on line <b>ZZZ</b><br />
<p>Some other content</p>
</div>';
preg_match('#<br \/>\n?(?<replace>.*)<br \/>#', $string_to_replace, $match);
$new = str_replace($match['replace'], 'text replaced!', $string_to_replace);
echo $new;
输出:
<div>
<p>Some content</p>
<br />
text replaced!<br />
<p>Some other content</p>
</div>
如何替换介于以下之间的所有内容:
<br />
<b>
和
<br />
例如:
<br />
<b>Notice</b>: Undefined variable: XXX in <b>YYY</b> on line <b>ZZZ<br />
<b>
注意:我知道我可以关闭错误报告。但在这种情况下,我需要用一些现有的 HTML 代码替换它们。
$string_to_replace = '<div>
<p>Some content</p>
<br />
<b>Notice</b>: Undefined variable: XXX in <b>YYY</b> on line <b>ZZZ</b><br />
<p>Some other content</p>
</div>';
$string_without_warnings = preg_replace('<br \/>(.*?)<br \/>', '', $string_to_replace);
使用正则表达式解析 html 不是个好主意。请参阅这个著名的 SO post 以了解原因。 RegEx match open tags except XHTML self-contained tags
话虽如此,您的要求当然是可能的,但我应该指出,根据您传入的内容,它可能表现得 differently/buggy,因此我们不鼓励这样做。
首先你的正则表达式:https://regex101.com/r/5fvuyi/1
<br \/>\n?(?<replace>.*)<br \/>
我使用了命名捕获组,您可以看到我在代码中引用了它。 https://3v4l.org/uSVYZ
<?php
$string_to_replace = '<div>
<p>Some content</p>
<br />
<b>Notice</b>: Undefined variable: XXX in <b>YYY</b> on line <b>ZZZ</b><br />
<p>Some other content</p>
</div>';
preg_match('#<br \/>\n?(?<replace>.*)<br \/>#', $string_to_replace, $match);
$new = str_replace($match['replace'], 'text replaced!', $string_to_replace);
echo $new;
输出:
<div>
<p>Some content</p>
<br />
text replaced!<br />
<p>Some other content</p>
</div>