943,670 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 693
  • PHP RSS
Feb 11th, 2009
0

nl2br question

Expand Post »
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
Similar Threads
Reputation Points: 15
Solved Threads: 2
Junior Poster
lifeworks is offline Offline
145 posts
since Nov 2007
Feb 11th, 2009
0

Re: nl2br question

While fetching, use str_replace to replace "<br />" with "".
ie.,
php Syntax (Toggle Plain Text)
  1. str_replace("<br />", "", $data);
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 11th, 2009
0

Re: nl2br question

Thanks nav!
Reputation Points: 15
Solved Threads: 2
Junior Poster
lifeworks is offline Offline
145 posts
since Nov 2007
Feb 11th, 2009
0

Re: nl2br question

You are welcome!
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 12th, 2009
0

Re: nl2br question

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:
PHP Syntax (Toggle Plain Text)
  1. $mytext = str_replace("<br />", "\n", $formatted_text);
  2. 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.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Feb 12th, 2009
1

Re: nl2br question

@Rhyan, \n will return extra line breaks. For example, If the actual string is
PHP Syntax (Toggle Plain Text)
  1. ab
  2. bc
\n will return
PHP Syntax (Toggle Plain Text)
  1. ab
  2.  
  3. bc
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 12th, 2009
0

Re: nl2br question

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...
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: cannot write file
Next Thread in PHP Forum Timeline: regex





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC