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

黄舟
Release: 2023-03-16 22:30:02
Original
1829 people have browsed it

Example

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

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

The browser output of the above code is as follows:

One line.
Another line.
Copy after login
Copy after login

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

One line.<br />
Another line.
Copy after login

Definition and usage

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

Syntax

nl2br(string,xhtml)
Copy after login

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

                FALSE - INSERT

Technical Details

##Return value: Return the converted string.

PHP Version: 4+

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

                  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);
?>
Copy after login

The browser output of the above code is as follows:

One line.
Another line.
Copy after login
Copy after login

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

One line.<br>
Another line.
Copy after login

A simple example:

<?php

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

echo nl2br($str);

?>
Copy after login

Run Resulting HTML code:

Welcome to <br />    
www.nowamagic.net
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template