开发者

Encode HTML entities but ignore HTML tags - in PHP

I have a string that might look like this

$str = "<p>Me & Mrs Jone开发者_Python百科s <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);


The problem, that you face, is that under circumstances you already have encoded '<' and '>' in your text, so you have to filter them out after conversion.

This is similar to Evert's answer, but adds one more step to allow for content like 1 < 2 in your markup:

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


A good answer was post by Pascal MARTIN

See this SO topic

To resume, you can use this piece of code, to retrieve a list of correspondances character => entity :

$list = get_html_translation_table(HTML_ENTITIES);
unset($list['"']);
unset($list['<']);
unset($list['>']);
unset($list['&']);


I haven't use htmlentities before, but it seems like a bit more robust version of urlencode (which I use a lot). You might want to try:

htmlentities(strip_tags($str,ENT_COMPAT),'UTF-8',false);

Just as a little nugget, if you want to preserve <br> as standard carrage returns, you could do this:

htmlentities(strip_tags(str_replace("<br>","\n",$str,ENT_COMPAT)),'UTF-8',false);

I know that's something I sometimes like to do.

Good Luck.


If you mean to convert only text, then try this:

$orig = "<p>Me & Mrs Jones <br /> live in <strong style="color:#FFF;">España</strong></p>";
$str = strip_tags($orig);

$str = htmlentities($str,ENT_COMPAT,'UTF-8',false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜