Selective HTML Decoding Problem
I have text in my DB that I want to display on the page. My 开发者_如何学运维DB content contains different tags eg. <html><b></b><help>
(because users are allowed to use any type of tags) etc.
When I display the content of the page, I want the <br>
tags decoded into spaces whereas other tags should remain like ordinary text.
Please, how do I go about it?
$content = preg_replace('/<br\s?\/?>/', ' ', $content);
echo html_entity_decode($content);
If you want <br>
converted to a space, and then all HTML displayed with their entities.
If I misinterpreted your question, and you don't want the HTML displayed with their equivalent entities, just skip the html_entity_decode()
function.
Update
Your comment to your OP now says you want the <br>
to be a line break, so simply switch that second argument of preg_replace()
from ' '
to "\n"
.
Update Again
Oh wait, I think I know what you want now. You want all tags to appear encoded, but the <br>
to actually be a line break (i.e. not encoded)? Well if you do switch your <br>
for \n
, and you have no other line breaks, you could throw a quick and dirty nl2br()
on the final markup.
Update
Okay, just do this to get your encoded <br>
back to a literal <br>
.
echo str_replace('<br>', '<br>', $content);
精彩评论