PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

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.



Home | Privacy Policy | Terms Of Use | Contact Us