tags:

views:

40

answers:

1

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?

+3  A: 
$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('&lt;br&gt;', '<br>', $content);
alex
mayb im nt too clear with my question:while inserting into the db i use htmlentities($content).while displaying, i want all <br> decoded and displaying other tags as just plain text tags
Ogugua Belonwu
hmmm.. thanks so far:i use the nl2br before inserting into the db.Maybe i should use it while displaying the selected data from the db?
Ogugua Belonwu
@alex - `<br class="reset" />`
Stephen P
@Stephen If he uses `nl2br()` on insertion into his database, it shouldn't be a problem. Of course, I wouldn't recommend inserting a encoded version into the database.
alex
I agree, I *never* encode before inserting into the DB. I want to store *exactly* what the user typed. I encode/escape after retrieval and depending on the intended use; i.e. providing a JSON response needs to be escaped differently than dropping the text directly into HTML.
Stephen P
thanks stephen, alex and Gert... the prob is solved for all new post. I changed this: $text=htmlentities(trim(utf8_decodenl2br(($_POST["txt"])), ENT_NOQUOTES)); to: $text=nl2br(htmlentities(trim(utf8_decode($_POST["txt"])), ENT_NOQUOTES));But i now have problems with previuos posts. they are now displaying <br> everywhere.
Ogugua Belonwu
@Stephen P You are *doing it right* :)
alex