PHP String nl2br() Function

PHP string nl2br() is predefine function which is used to insert HTML line break before all newlines in a string. It returns string with <br \> or <br> before all newlines (\r\n, \n\r, \n and \r).

Syntax:

ParameterDescriptionRequired/Optional
StringSpecify input stringrequired
xhtmlBoolean value, whether to use XHTML compatible line breaks or not. .True- Default. Inserts <br /> .False -Insert <br>optional

Example 1

Note: Generating valid HTML markup by using 'is_xhtml' parameter.

Output:

Your string is: Hello .\ n PHP. \ n Javatpoint
By using 'nl2br()' function: Hello .
PHP.
Javatpoint

Example 2

Output:

Your string is: Welcome \ r\ n We are learning PHP
By using 'nl2br()' Function: 
Welcome
We are learning PHP

Example 3

Output:

By using 'nl2br()' Function:This
is
a
PHP Tutorial

Next TopicPHP String




Latest Courses