在PHP中对HTML实体进行编码,但忽略HTML标记 [英] Encode HTML entities but ignore HTML tags - in PHP

查看:140
本文介绍了在PHP中对HTML实体进行编码,但忽略HTML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可能看起来像这样的字符串

  $ str =< p> Me& Mrs Jones< ; < strong style =color:#FFF;>España< / strong>< / p>; 
htmlentities($ str,ENT_COMPAT,'UTF-8',false);

如何在不转换HTML标签的情况下将文本转换为HTML实体?



注意:我需要保持HTML不变

免责声明:我不会编码任何实体,除了<,>和& ;.这就是说,如果你真的想这样做,请这样做:

  $ str ='.. '; 
$ str = htmlentities($ str,ENT_NOQUOTES,'UTF-8',false);
$ str = str_replace(array('& lt;','& gt;'),array('<','>'),$ str);


I have a string that might look like this

$str = "<p>Me & Mrs Jones <br /> live in <strong style="color:#FFF;">España</strong></p>";
htmlentities($str,ENT_COMPAT,'UTF-8',false);

How can I convert the text to HTML entities without converting the HTML tags?

note: I need to keep the HTML intact

解决方案

Disclaimer: I would not encode any entities, except for <, > and &. That said, if you really want this, do this:

$str = '...';
$str = htmlentities($str,ENT_NOQUOTES,'UTF-8',false);
$str = str_replace(array('&lt;','&gt;'),array('<','>'), $str);

这篇关于在PHP中对HTML实体进行编码,但忽略HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆