php function nl2br () to insert HTML newline characters before each line in a string

Source: Internet
Author: User

Instance

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

<?phpecho nl2br ("One line.\nanother line.");? >

The browser output of the above code is as follows:

One line. Another line.

The HTML input for the above code is as follows (see source code):

One line.<br/>another line.

Definition and usage

The NL2BR () function inserts an HTML line break (<br> or <br/>) before each new line (\ n) in the string.

Grammar

NL2BR (string,xhtml)

Parameter description

string is required. Specifies the string to check.

XHTML is optional. A Boolean value that indicates whether to use compatible XHTML wrapping:

true-default. inserting <br/>

FALSE-Insert <br>

Technical details

Return value: Returns the converted string.

PHP version: 4+

Update log: Before PHP 4.0.5, this function inserts <br>. After PHP 4.0.5, this function inserts <BR/> compatible with XHTML.

In PHP 5.3, a new XHTML parameter was added.

More examples

Example 1

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

<?phpecho nl2br ("One line.\nanother line.", false);? >

The browser output of the above code is as follows:

One line. Another line.

The HTML input for the above code is as follows (see source code):

One line.<br>another line.

A simple example:

<?php$str = "Welcome to Www.nowamagic.net"; Echo nl2br ($STR);? >

HTML code to run the result:

Welcome to <br/>    www.nowamagic.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.