Hướng dẫn dùng nl2br trong PHP

Hàm nl2br[] sẽ thêm các thẻ xuống dòng [
]  vào trước khi bắt đầu dòng mới trong chuỗi [ \r, \n, \n, \r, \n and \r].

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú phápnl2br[ $str, $is_xhtml];

Trong đó:

  • $str là chuỗi cần thêm các thẻ
    .
  • $is_xhtml là tham số không bắt buộc mặc định là TRUE, quy định xem có xuống dòng tương thích với XHTML hay không.

Ví dụ

Code

echo "freetuts.net\n PHP functionn";
echo "
"; echo nl2br["freetuts.net\n PHP functionn"];

Kết quả

freetuts.net PHP functionn
freetuts.net
PHP functionn

Code

$string = "This\r\nis\n\ra\nstring\r";
echo nl2br[$string];

Tham khảo: php.net

Bài viết này được đăng tại [free tuts .net]

❮ Tham chiếu chuỗi PHP

Thí dụ

Chèn ngắt dòng nơi xuất hiện dòng mới [\ n] trong chuỗi:

Đầu ra trình duyệt của đoạn mã trên sẽ là:

One line.
Another line.

Đầu ra HTML của đoạn mã trên sẽ là [Xem Nguồn]:

One line.

Another line.

Định nghĩa và Cách sử dụng

Hàm nl2br [] chèn các ngắt dòng HTML [
hoặc
] vào trước mỗi dòng mới [\ n] trong một chuỗi.

Cú pháp

Giá trị tham số

ParameterDescription
string Required. Specifies the string to check
xhtml  Optional. A boolean value that indicates whether or not to use XHTML compatible line breaks:
  • TRUE- Default. Inserts
  • FALSE - Inserts

Chi tiết kỹ thuật

Giá trị trả lại:Phiên bản PHP:Changelog:
Trả về chuỗi đã chuyển đổi
4+
Tham số xhtml đã được thêm vào PHP 5.3.
Trước PHP 4.0.5, nó đã chèn
.
Sau PHP 4.0.5, nó sẽ chèn
.

Các ví dụ khác

Thí dụ

Chèn ngắt dòng khi xuất hiện dòng mới [\ n], sử dụng tham số xhtml:

Đầu ra trình duyệt của đoạn mã trên sẽ là:

One line.
Another line.

Đầu ra HTML của đoạn mã trên sẽ là [Xem Nguồn]:

One line.

Another line.

❮ Tham chiếu chuỗi PHP


[PHP 4, PHP 5, PHP 7, PHP 8]

nl2brInserts HTML line breaks before all newlines in a string

Description

nl2br[string $string, bool $use_xhtml = true]: string

Parameters

string

The input string.

use_xhtml

Whether to use XHTML compatible line breaks or not.

Return Values

Returns the altered string.

Examples

Example #1 Using nl2br[]

Collette

2 years ago

Let's say a form text field does contain:
test line number 1
test line number 2

To remove all the line breaks and split the input into an array:

$input_from_text_field = preg_replace['#[\r\n]#','',nl2br[$input_from_text_field,false]];
$lines = explode["
",$input_from_text_field];

$lines will now be an array where $lines[0] = "test line number 1" and so on.

buzanits at gmail dot com

4 years ago

If you write code that is to run in browser AND on the shell, this function could be useful. It uses PHP_EOL or
depending on the platform it runs:

leo dot mauro dot desenv at gmail dot com

6 years ago

I test empirically this function nl2br and nl2br2 [create by ngkongs at gmail dot com].
Both work nice with different ASCII chars for linebreak, but the function nl2br2 is faster than nl2br.

nl2br2 ~ 0.0000309944153 s
nl2br  ~ 0.0011141300201 s

The function nl2br2:

Justinas M.

6 years ago

This is example with "\R" regex token which matches any unicode newline character.
"u" flag treate strings as UTF-16. Which is optional, depending on your use case.



NOTE:
preg_replace versions are much slower than using str_replace version or built-in nl2br.
Check out pcre.backtrack_limit php.ini setting for information about PCRE limit. It's good to know.

Some PHP7 benchmarks:


# nl2br
## Time: 0.02895712852478 s

# nl2br_str
## Time: 0.027923107147217 s

# nl2br_preg_R
## Time: 0.13350105285645 s

# nl2br_preg_rnnr
## Time: 0.14213299751282 s

chad at nobodyfamous dot ca

5 years ago

I just spent a whole day trying to figure out why my textarea $_POST was not getting
tags added by nl2br[].  So for others

returns newlines as  and so nl2br will miss them.

rather use and newlines will remain intact so nl2br will pick them up.

blacknine313 at gmail dot com

14 years ago

After a recent post at the forums on Dev Shed, I noticed that it isn't mentioned, so I will mention it.

nl2br returns pure HTML, so it should be after PHP anti-HTML functions [ such as strip_tags and htmlspecialchars ].

darenschwenke at yahoo dot com

8 years ago

This one works with br tags having attributes, in any case,
closed or  not closed, and does not double linefeeds



I combine this with strip_tags[] for dead simple "contenteditable" fields allowing only text and linefeeds.

j dot mons54 at gmail dot com

10 years ago

for bbcode :

hyponiq at gmail dot com

15 years ago

On the contrary, mark at dreamjunky.comno-spam, this function is rightfully named.  Allow me to explain.  Although it does re-add the line break, it does so in an attempt to stay standards-compliant with the W3C recommendations for code format.

According to said recommendations, a new line character must follow a line break tag.  In this situation, the new line is not removed, but a break tag is added for proper browser display where a paragraph isn't necessary or wanted.

Chủ Đề