tags:

views:

77

answers:

3

I am using this to adapt my text which then gets inserted into a mysql db:

$ad_text=nl2br(wordwrap($_POST['annonsera_text'], 60, "\n", true));

When users wish to change their posting, they click a link on my page, and a form opens. In this form, the textarea where I fetch the info from mysql, I am displaying the text again. Only problem is, the text itself contains '<br>' tags. That is, it shows up EXACTLY as it looks in the mysql table field.

How can I undo the function above so that the <br> tags are removed again? Thanks

A: 

A quick find and replace would do the trick:

$ad_text = str_replace('<br />', "\n", $ad_text);

...essentially replacing all found breaks with newlines.

cballou
+5  A: 

Why not insert your clean unmodified text into the database, then html-ify it only when displaying?

Derek Illchuk
Displaying the text would most likely happen more often than editing. The OP's approach reduces work required for the normal case.
Jeff Paquette
What the heck? Why was this answer chosen? It doesn't solve his current problem it just tells him how to fix it for the future...
KyleFarris
+1  A: 

This will be more future-proof, I think:

$ad_text = preg_replace('/<br\s*?/?>/i', "\n", $ad_text);

You never know... the nl2br script may or may not put spaces in between the <br and the /> in the future.

KyleFarris