Adding line breaks to text

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
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Adding line breaks to text

Post by cbrian »

How would I add line breaks to a text area after it is submitted like this:

Text

Text2
Text3

would be

Text<br>
<br>
Text2<br>
Text3<br>
marike
Forum Newbie
Posts: 24
Joined: Thu Mar 31, 2005 6:19 pm
Location: New York

Use nl2br

Post by marike »

Unless I'm missing something, you could use nl2br like so:

Code: Select all

<?php
$string = "Text\nText2\nText3\n";

echo nl2br($string);
?>

And if you have more sophisticated needs it would be pretty easy to write a function that uses nl2br. Hope this helps.
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

That works. Thanks!
Post Reply