PHP nl2br() Function with Example

PHP nl2br() work: Here, we will find out about the nl2br() work with the example in PHP.

PHP nl2br() work

nl2br() means “New Line to Break”, this capacity changes over all new line characters (\n) to break tag (
) in the string. It very well may be seen by survey the wellspring of the HTML.

Syntax:

    nl2br(string, [xhtml]);

It acknowledges the string and changes over returns the string with tag rather than all \n (Newline character).

Here, second parameter XHTML is a discretionary parameter and it is utilized to either utilize
– if XHTML value is FALSE, or use
– if XHTML value is TRUE. In the event that you don’t utilize this parameter, the default value will be TRUE.

Example:

    Input: "Hello\nWorld"
    Output:
    Hello<br />
    World

PHP Code:

<?php
    $str = "Hello\nWorld\n";
    echo nl2br($str);

    $str = "My\nName\nis\JustTechReview\n";
    echo nl2br($str);

    $str = "Hello\nWorld\n";
    echo nl2br($str,FALSE);

    $str = "My\nName\nis\JustTechReview\n";
    echo nl2br($str,FALSE);    
?>

Output:

Hello<br />
World<br />
My<br />
Name<br />
is<br />
JustTechReview<br />
Hello<br>
World<br>
My<br>
Name<br>
is<br>
JustTechReview<br>

Leave a Comment

error: Alert: Content is protected!!