Ale*_*lex 11 php string html-encode html-entities
基本上我想要像这样转换一个字符串:
<code> <div> blabla </div> </code>
进入这个:
<code> <div> blabla </div> </code>
我该怎么做?
像这样的页面,其中包含允许的HTML标记和示例列表.例如,<code>是一个允许的标签,这将是样本:
<code><?php echo "Hello World!"; ?></code>
Run Code Online (Sandbox Code Playgroud)
我想要一个反向函数,因为有很多这样的标签带有样本,我将它们全部存储到一个数组中,我在一个循环中迭代,而不是单独处理每个...
我的版本使用正则表达式:
$string = '<code> <div> blabla </div> </code>';
$new_string = preg_replace(
'/(.*?)(<.*?>|$)/se',
'html_entity_decode("$1").htmlentities("$2")',
$string
);
Run Code Online (Sandbox Code Playgroud)
它尝试匹配每个标签和文本节点,然后分别应用htmlentities和html_entity_decode。