Home  >  Article  >  Backend Development  >  php function nl2br() that inserts html newline characters before each line in the string

php function nl2br() that inserts html newline characters before each line in the string

黄舟
黄舟Original
2017-11-02 13:41:361753browse

Example

Insert a newline character before a new line (\n) in the string:

<?php
echo nl2br("One line.\nAnother line.");
?>

The browser output of the above code is as follows:

One line.
Another line.

The above code The HTML input is as follows (view source code):

One line.<br />
Another line.

Definition and usage

nl2br() function inserts an HTML newline character (<) before each new line (\n) in the string ;br> or df250b2156c434f3390392d09b1c9563).

Syntax

nl2br(string,xhtml)

Parameter Description

string Required. Specifies the string to check. ​

xhtml ​ Optional. One indicates whether to use the Boolean value compatible with XHTML:

True-default. INSERT df250b2156c434f3390392d09b1c9563

                FALSE - INSERT 0c6dc11e160d3b678d68754cc175188a

Technical Details

##Return value: Return the converted string.

PHP Version: 4+

##Change Log: Before PHP 4.0.5 , this function inserts 0c6dc11e160d3b678d68754cc175188a. As of PHP 4.0.5, this function inserts an XHTML-compatible df250b2156c434f3390392d09b1c9563.

                  In PHP 5.3, a new xhtml parameter was added.

More examples

Example 1

Insert a newline character before a new line (\n) by using the xhtml parameter:

<?php
echo nl2br("One line.\nAnother line.",false);
?>

The browser output of the above code is as follows:

One line.
Another line.

The HTML input of the above code is as follows (view source code):

One line.<br>
Another line.

A simple example:

<?php

$str = "Welcome to 
www.nowamagic.net";

echo nl2br($str);

?>

Run Resulting HTML code:

Welcome to <br />    
www.nowamagic.net

The above is the detailed content of php function nl2br() that inserts html newline characters before each line in the string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn