PHP Phrasebook

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.

Категории