Hi

Ive been using the nl2br function on text that users input... which has been great, but if they want to edit that text later, its riddled with <br />'s etc... is there a way of stripping these html tags out of the code before its displayed in the 'edit' textarea?

Thanks
lworks

Recommended Answers

All 6 Replies

While fetching, use str_replace to replace "<br />" with "".
ie.,

str_replace("<br />", "", $data);

Thanks nav!

You are welcome!

Member Avatar for Rhyan

Nav33n's proposal will return a string with no spaces, instead with new lines where the breaks are.
In order to return a string with line breaks, you should replace <br /> with \n , store it in a variable and then return the variable value as evaluated string.

Doing it like this:

$mytext = str_replace("<br />", "\n", $formatted_text);
echo "$mytext";

The above should validate the variable content whether it contains php code and execute it. So, the \n will actually cause the string to be returned with new lines in it.

@Rhyan, \n will return extra line breaks. For example, If the actual string is

ab
bc

\n will return

ab 

bc
commented: sounds right to me :) +14
Member Avatar for Rhyan

Hm,
Appears you are right.
It really does not replace new line indicator by html equivalent <br /> it only inserts it before the new line indicator. Odd....
So instead using nl2br, you can use str_replace ("\n", '<br />', $string) to get the new lines correctly substituted....
Obviously the decision how would you do it depends on which one is faster.
So, I suppose nl2br should be faster when reverting to plain text...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.