downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

number_format> <nl_langinfo
[edit] Last updated: Fri, 14 Jun 2013

view this page in

nl2br

(PHP 4, PHP 5)

nl2br在字符串所有新行之前插入 HTML 换行标记

说明

string nl2br ( string $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

更新日志

版本 说明
5.3.0 新增可选的 is_xhtml 参数。
4.0.5 nl2br() 成为 XHTML 兼容的操作。所有的旧版本都会在 string 的新行之前插入 '<br>' 而不是 '<br />'。

参见



number_format> <nl_langinfo
[edit] Last updated: Fri, 14 Jun 2013
 
add a note add a note User Contributed Notes nl2br - [9 notes]
up
3
j dot mons54 at gmail dot com
1 year ago
for bbcode :

<?php
$message
nl2br(preg_replace('#(\\]{1})(\\s?)\\n#Usi', ']', stripslashes($message)));
?>
up
3
blacknine313 at gmail dot com
5 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 ).
up
1
ngkongs at gmail dot com
6 years ago
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
up
3
Anders Norrbring
7 years ago
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);
}
?>
up
2
N/A
4 years ago
Here's a more simple one:

<?php
/**
 * Convert BR tags to nl
 *
 * @param string The string to convert
 * @return string The converted string
 */
function br2nl($string)
{
    return
preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
}
?>

Enjoy
up
0
Joshua Anderson
3 years ago
This function will change new lines (line breaks) to <br/> and it allows you to limit the amount of brs allowed at any point in time.

This function was made to avoid people spaming a textarea with hundreds of line breaks or empty lines.

<?php
function nl2br_limit($string, $num){
   
$dirty = preg_replace('/\r/', '', $string);
$clean = preg_replace('/\n{4,}/', str_repeat('<br/>', $num), preg_replace('/\r/', '', $dirty));
   
return
nl2br($clean);
}

echo
nl2br_limit($string,'4');
?>

// Heres how it works //

nl2br_limit($string, $num)
// $string is the entered text you want to strip lines out of, it could be ($_POST['myinput'])
// $num is the amount of consecutive <br/>'s that are allowed any at a time.

The user is allowed to enter as many line breaks as $num allows
up
0
hyponiq at gmail dot com
5 years ago
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.
up
0
CGameProgrammer at gmail dot com
8 years ago
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 );
?>
up
-2
mail at fort-hub dot com
2 years ago
I noticed that I am using an earlier version of PHP which does not recognise the $xhtml parameter in nl2br(), therefore I am stuck with xhtml line breaks, which I do not want. Thus I made the following simple function:

<?php

   
function NlToBr($inString)
    {
        return
preg_replace("%\n%", "<br>", $inString);
    }

?>

 
show source | credits | sitemap | contact | advertising | mirror sites