如果您的网站严重依赖 PRE 标签,您就会知道将 PRE 标签内容转换为 HTML 实体的重要性。这样做可以防止可能出现的渲染问题。以下 PHP 片段 HTML-Entitizes (?) PRE 标记中的任何代码:
//replaces pre content with html entities function pre_entities($matches) { return str_replace($matches[1],htmlentities($matches[1]),$matches[0]); } //to html entities; assume content is in the "content" variable $content = preg_replace_callback('/<pre.*?>(.*?)<\/pre>/imsu',pre_entities, $content);
我几乎在我写的每一篇文章中都使用它。如果您有可能使用 PRE 标签的客户,您也可以将其粘贴到您的 CMS 中。