nl2br
(PHP 4, PHP 5)
nl2br — Inserts HTML line breaks before all newlines in a string
Description
string nl2br
( string
$string
[, bool $is_xhtml
= true
] )
Returns string
with '<br />' or
'<br>' inserted before all newlines (\r\n,
\n\r, \n and \r).
Parameters
-
string
-
The input string.
-
is_xhtml
-
Whether to use XHTML compatible line breaks or not.
Return Values
Returns the altered string.
Examples
Example #1 Using nl2br()
<?php
echo nl2br("foo isn't\n bar");
?>
The above example will output:
foo isn't<br /> bar
Example #2 Generating valid HTML markup using the is_xhtml
parameter
<?php
echo nl2br("Welcome\r\nThis is my HTML document", false);
?>
The above example will output:
Welcome<br> This is my HTML document
Example #3 Various newline separators
<?php
$string = "This\r\nis\n\ra\nstring\r";
echo nl2br($string);
?>
The above example will output:
This<br /> is<br /> a<br /> string<br />
Changelog
Version | Description |
---|---|
5.3.0 |
Added the optional is_xhtml parameter.
|
4.0.5 |
nl2br() is now XHTML compliant. All older versions
will return string with '<br>' inserted
before newlines instead of '<br />'.
|
See Also
- htmlspecialchars() - Convert special characters to HTML entities
- htmlentities() - Convert all applicable characters to HTML entities
- wordwrap() - Wraps a string to a given number of characters
- str_replace() - Replace all occurrences of the search string with the replacement string
- addcslashes
- addslashes
- bin2hex
- chop
- chr
- chunk_split
- convert_cyr_string
- convert_uudecode
- convert_uuencode
- count_chars
- crc32
- crypt
- echo
- explode
- fprintf
- get_html_translation_table
- hebrev
- hebrevc
- hex2bin
- html_entity_decode
- htmlentities
- htmlspecialchars_decode
- htmlspecialchars
- implode
- join
- lcfirst
- levenshtein
- localeconv
- ltrim
- md5_file
- md5
- metaphone
- money_format
- nl_langinfo
- nl2br
- number_format
- ord
- parse_str
- printf
- quoted_printable_decode
- quoted_printable_encode
- quotemeta
- rtrim
- setlocale
- sha1_file
- sha1
- similar_text
- soundex
- sprintf
- sscanf
- str_getcsv
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_shuffle
- str_split
- str_word_count
- strcasecmp
- strchr
- strcmp
- strcoll
- strcspn
- strip_tags
- stripcslashes
- stripos
- stripslashes
- stristr
- strlen
- strnatcasecmp
- strnatcmp
- strncasecmp
- strncmp
- strpbrk
- strpos
- strrchr
- strrev
- strripos
- strrpos
- strspn
- strstr
- strtok
- strtolower
- strtoupper
- strtr
- substr_compare
- substr_count
- substr_replace
- substr
- trim
- ucfirst
- ucwords
- vfprintf
- vprintf
- vsprintf
- wordwrap
Коментарии
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 );
?>
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);
}
?>
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
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
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
/**
* Convert BR tags to newlines and carriage returns.
*
* @param string The string to convert
* @return string The converted string
*/
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
/**
* Convert BR tags to newlines and carriage returns.
*
* @param string The string to convert
* @param string The string to use as line separator
* @return string The converted string
*/
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);
}
?>
double quotes !== single quotes
php > echo nl2br('\r\n');
\r\n
php > echo nl2br("\r\n");
<br />