nl2br question

Thread Solved

Join Date: Nov 2007
Posts: 91
Reputation: lifeworks is an unknown quantity at this point 
Solved Threads: 2
lifeworks lifeworks is offline Offline
Junior Poster in Training

nl2br question

 
0
  #1
Feb 11th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: nl2br question

 
0
  #2
Feb 11th, 2009
While fetching, use str_replace to replace "<br />" with "".
ie.,
  1. str_replace("<br />", "", $data);
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 91
Reputation: lifeworks is an unknown quantity at this point 
Solved Threads: 2
lifeworks lifeworks is offline Offline
Junior Poster in Training

Re: nl2br question

 
0
  #3
Feb 11th, 2009
Thanks nav!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: nl2br question

 
0
  #4
Feb 11th, 2009
You are welcome!
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: nl2br question

 
0
  #5
Feb 12th, 2009
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:
  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.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: nl2br question

 
1
  #6
Feb 12th, 2009
@Rhyan, \n will return extra line breaks. For example, If the actual string is
  1. ab
  2. bc
\n will return
  1. ab
  2.  
  3. bc
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: nl2br question

 
0
  #7
Feb 12th, 2009
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...
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 423 | Replies: 6
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC