|
| HOW TO USE THE nl2br() FUNCTION IN PHP
The nl2br() function places a HTML line break (<br />) in front of every new line (\n) character or new line it encounters.
The general format is:
nl2br(string);
Example 1: This string has the new line character (\n) clearly shown.
$string="Hello\nMy name\nis\nJohn";
$new_string=nl2br($string);
echo $new_string;
The above outputs:
Hello
My name
is
John
The HTML code (look at the page source in your browser) looks like:
Hello<br />
My name<br />
is<br />
John
Example 2: This string is over a few new lines.
$string="Hello
My name
is
John";
$new_string=nl2br($string);
echo $new_string;
The above outputs:
Hello
My name
is
John
The HTML code (look at the page source in your browser) looks like:
Hello<br />
My name<br />
is<br />
John
The same results are achieved with Example 1 and 2 irrespective whether the newline character(\n) is shown or whether the string is over a few new lines.
|
|