使用htmlspecialchar()保留字符含义,但省略<;br/>;在输出显示中


using htmlspecialchars() to preserve character meaning but omit <br/> in output display

我需要格式化字符串

$string = "<em>Hello</em><br/>world";

显示为:

<em>Hello</em>
world

<br/>'n等字符外,所有html字符都应按原样显示以保留其含义

<br/>标签仍然应该做它应该做的事情,打破界限,但所有其他标签都需要如上所示。

我想用这样的东西来实现这一点:

htmlspecialchars($string); // except for the <br/> or 'n tags

$string值需要以如下方式插入到数据库表中,即输出将显示为如上所示。

使用PHP,实现的最佳方法是什么

尝试用'n 替换<br>

$html = '<em>Hello</em><br/>world';
$nl = preg_replace('#<br's*/?>#i', "'n", $html);
echo $nl;

来源:https://stackoverflow.com/a/2436181/552807