user188962
user188962

Reputation:

How to 'undo' this small function in PHP?

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

Upvotes: 0

Views: 232

Answers (3)

KyleFarris
KyleFarris

Reputation: 17538

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.

Upvotes: 1

Derek Illchuk
Derek Illchuk

Reputation: 5658

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

Upvotes: 5

Corey Ballou
Corey Ballou

Reputation: 43457

A quick find and replace would do the trick:

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

...essentially replacing all found breaks with newlines.

Upvotes: 0

Related Questions