Using Line Breaks


How can a line break be used within HTML? That's easy: with the <br /> HTML element. However, what if there is data with \n or \r\n line breaks? Search and replace comes to mind; however, it is much easier to use a built-in PHP function: nl2br(). This parses a string and converts all line breaks to <br /> elements, as the preceding script shows.

Adding <br /> Elements at Every Line Break (nl2br.php)
 <?php   $input = "One\nTwo\r\nThree";   echo nl2br($input); ?> 

Outputs the HTML:

 One<br /> Two<br /> Three 

As you can see, the line breaks are still there, but the <br /> elements were added.




PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net