【问题标题】:PHP - convert to html decimal codes while preserving tagsPHP - 在保留标签的同时转换为 html 十进制代码
【发布时间】:2016-07-19 19:22:34
【问题描述】:

我想转换诸如

之类的字符
<p>Glückwunsch</p>

<p>Gl&#252;ckwunsch</p>

同时还保留 html 标签。

知道如何做到这一点吗? 谢谢。

【问题讨论】:

标签: php html


【解决方案1】:

我认为一个好的解决方案是这样的:

$dom = new DOMDocument();
$dom->loadHTML($string);
foreach($dom->getElementsByTagName('*') as $node ){
    $node->textContent = htmlentities($node->textContent);
}
$string = $dom->saveXML($dom);

这样您可以避免弄乱以下内容: &lt;p&gt; We know that 2 &amp;lt; 4&lt;/p&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2013-04-21
    • 2015-09-16
    • 2012-10-13
    • 2018-07-12
    • 2013-02-19
    • 2017-06-28
    相关资源
    最近更新 更多