Home » PHP

PHP nl2br() Function with Example

PHP nl2br() function: Here, we are going to learn about the nl2br() function with example in PHP.
Submitted by IncludeHelp, on February 05, 2019

PHP nl2br() function

nl2br() stands for "New Line to Break", this function converts all new line characters (\n) to break tag (<br>) in the string. It can be seen by viewing the source of the HTML.

Syntax:

    nl2br(string, [xhtml]);

It accepts the string and converts returns the string with <br> tag instead of all \n (New line character).

Here, second parameter xhtml is an optional parameter and it is used to either use <br> - if xhtml value is FALSE, or use <br /> - if xhtml value is TRUE. If you don't use this parameter, the default value will be TRUE.

Examples:

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

PHP code:

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

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

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

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

Output

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.