©
本文檔使用 php中文網手册 發布
(PHP 4, PHP 5)
nl2br — 在字符串所有新行之前插入 HTML 换行标记
$string
[, bool $is_xhtml
= true
] )
在字符串 string
所有新行之前插入 '<br />' 或 '<br>',并返回。
string
输入字符串。
is_xhtml
是否使用 XHTML 兼容换行符。
返回调整后的字符串。
Example #1 nl2br() 使用范例
<?php
echo nl2br ( "foo isn't\n bar" );
?>
以上例程会输出:
foo isn't<br /> bar
Example #2 使用 is_xhtml
生成合法的 HTML 标记
<?php
echo nl2br ( "Welcome\r\nThis is my HTML document" , false );
?>
以上例程会输出:
Welcome<br> This is my HTML document
Example #3 各种换行分隔符
<?php
$string = "This\r\nis\n\ra\nstring\r" ;
echo nl2br ( $string );
?>
以上例程会输出:
This<br /> is<br /> a<br /> string<br />
版本 | 说明 |
---|---|
5.3.0 |
新增可选的 is_xhtml 参数。
|
4.0.5 | nl2br() 成为 XHTML 兼容的操作。所有的旧版本都会在 string 的新行之前插入 '<br>' 而不是 '<br />'。
|
[#1] leo dot mauro dot desenv at gmail dot com [2015-09-17 12:36:28]
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:
<?php
function nl2br2($string) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $string;
}
?>
[#2] fquffio at live dot it [2014-06-10 08:46:48]
Starting from PHP 4.3.10 and PHP 5.0.2, this should be the most correct way to replace <br /> and <br> tags with newlines and carriage returns.
<?php
function br2nl ( $string )
{
return preg_replace('/\<br(\s*)?\/?\>/i', PHP_EOL, $string);
}
?>
(Please note this is a minor edit of this function: http://php.net/nl2br#86678 )
You might also want to be "platform specific", and therefore this function might be of some help:
<?php
function br2nl ( $string, $separator = PHP_EOL )
{
$separator = in_array($separator, array("\n", "\r", "\r\n", "\n\r", chr(30), chr(155), PHP_EOL)) ? $separator : PHP_EOL; // Checks if provided $separator is valid.
return preg_replace('/\<br(\s*)?\/?\>/i', $separator, $string);
}
?>
[#3] billhicks at yahoo dot com [2014-03-20 06:16:03]
Your new site does not work in ie.
But you already know this and I guess you don't really care.
its all about style now - funny because this is a php site not a css/java/ajax site.
piss off!!!!!!!!!!!!!!!
[#4] darenschwenke at yahoo dot com [2014-01-28 19:15:57]
This one works with br tags having attributes, in any case,
closed or not closed, and does not double linefeeds
<?php
function br2nl($string)
{
return preg_replace("/<br[^>]*>\s*\r*\n*/is", "\n", $string);
}
?>
I combine this with strip_tags() for dead simple "contenteditable" fields allowing only text and linefeeds.
[#5] j dot mons54 at gmail dot com [2012-05-29 21:45:48]
for bbcode :
<?php
$message = nl2br(preg_replace('#(\\]{1})(\\s?)\\n#Usi', ']', stripslashes($message)));
?>
[#6] N/A [2008-10-29 07:41:41]
Here's a more simple one:
<?php
function br2nl($string)
{
return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
}
?>
Enjoy
[#7] blacknine313 at gmail dot com [2008-04-01 18:34:36]
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 ).
[#8] hyponiq at gmail dot com [2007-08-25 23:03:24]
On the contrary, <b>mark at dreamjunky.comno-spam</b>, 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.
[#9] ngkongs at gmail dot com [2007-02-23 04:12:39]
to replace all linebreaks to <br />
the best solution (IMO) is:
<?php
function nl2br2($string) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $string;
}
?>
because each OS have different ASCII chars for linebreak:
windows = \r\n
unix = \n
mac = \r
works perfect for me
[#10] Anders Norrbring [2006-03-09 13:28:48]
Seeing all these suggestions on a br2nl function, I can also see that neither would work with a sloppy written html line break.. Users can't be trusted to write good code, we know that, and mixing case isn't too uncommon.
I think this little snippet would do most tricks, both XHTML style and HTML, even mixed case like <Br> <bR /> and even <br > or <br />.
<?php
function br2nl($text)
{
return preg_replace('/<br\\s*?\/??>/i', '', $text);
}
?>
[#11] CGameProgrammer at gmail dot com [2005-01-30 17:28:13]
It's important to remember that this function does NOT replace newlines with <br> tags. Rather, it inserts a <br> tag before each newline, but it still preserves the newlines themselves! This caused problems for me regarding a function I was writing -- I forgot the newlines were still being preserved.
If you don't want newlines, do:
<?php
$Result = str_replace( "\n", '<br />', $Text );
?>