What could you use for br2nl?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

What could you use for br2nl?

Post by sinewave »

i know there isnt a br2nl() but i have a textarea that reads and writes to a txtfile, but when i use nl2br() to put the breaks in the txt, that's fine.

When i read from the txtfile it will put the "<br />" back into the textarea (obviously), but i'd prefer it put new lines there. The people who will use the text area to update certain sections will know no html. Any advice would be great.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You shouldn't need to do nl2br() unless you are about to display the data (outside of a textarea) as nl2br() just puts <br />'s next to newline characters - it doesn't replace one with the other. So, in your text file just store the text with only the newline characters, you don't need to store both.

However, getting rid of the stored <br />'s is nice and easy thanks to str_replace():

Code: Select all

$text = str_replace('&lt;br /&gt;', '', $text);
Mac
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

well teh textarea goes to a txt file, and then another page uses the txtfile to display it's contents in html. so if they press enter 5 times i want it to put 5 <br />, i dont know how else you could do that without nl2br()

but also i want it to go backwards too...if the txtfile has <br /> 5 times i want the text area to have 5 line breaks

does this make sense?

could you str_replace with \n or something to that effect?
superwormy
Forum Commoner
Posts: 67
Joined: Fri Oct 04, 2002 9:25 am
Location: CT

Post by superwormy »

$string = str_replace ("<br />", "\n", $string);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Using nl2br() does not remove the newline characters it only puts the HTML line break next to it so if you had

Code: Select all

line1 \n line2
after using nl2br() you would have

Code: Select all

line 1 &lt;br /&gt;\n line2
so to get rid of the HTML line breaks use the code I showed before:

Code: Select all

$text = str_replace('&lt;br /&gt;', '', $text);
and you'll have

Code: Select all

line 1 \n line2
again. If you use superwormy's code you'll end up with double newline characters:

Code: Select all

line1 \n\n line2
Mac
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

perfect. thanks
Post Reply