<?php
str nl2br ( str $string [, bool $is_xhtml = TRUE ] )
where,
$string = The input STRING
$is_xhtml = To control if use XHTML
compatible line breaks or not
?>
BOOLEAN | MEANING |
TRUE | XHTML compatible line breaks will be used |
FALSE | NOT XHTML compatible line breaks will be used |
ed48 |
<?php
// ORIGINAL TEXT
$origtxt = "Admit impediments; love is not love
it is the star to every wand'ring bark, love's
not time's fool,
though rosy lips and cheeks.
\nWhose worth's unknown, although his height be taken.
\nBut bears it out even to the edge of doom.
\n Oh, no, it is an ever fixed mark that looks
on tempests and is never shaken;
\nit is the star to every wand'ring bark.
\nOr bends with the remover to remove.";
echo 'ORIGINAL TEXT:<br>' . $origtxt;
echo '<br><br>';
// RESULTING TEXT
$resltxt = nl2br($origtxt);
echo 'RESULTING TEXT:<br>' . $resltxt;
?>
<?php
var_dump(nl2br("test"));
var_dump(nl2br(""));
var_dump(nl2br(NULL));
var_dump(nl2br("\r\n"));
var_dump(nl2br("\n"));
var_dump(nl2br("\r"));
var_dump(nl2br("\n\r"));
var_dump(nl2br("\n\r\r\n\r\r\r\r"));
var_dump(nl2br("\n\r\n\n\r\n\r\r\n\r\n"));
var_dump(nl2br("\n\r\n\n\n\n\r\r\r\r\n\r"));
?>
<?php
/* Test nl2br() function by passing
* double quoted strings containing various
* combinations of new line chars to 'str' argument
*/
echo "Testing nl2br() : usage variations.<br>";
$strings = array(
//new line chars embedded in strings
"Hello\nWorld",
"\nHello\nWorld\n",
"Hello\rWorld",
"\rHello\rWorld\r",
"Hello\r\nWorld",
"\r\nHello\r\nWorld\r\n",
//one blank line
"
",
//two blank lines
"
",
//inserted new line in a string
"Hello
World"
);
//loop through $strings array to test nl2br()
// function with each element
$count = 1;
foreach( $strings as $str ){
echo "<br><br>Iteration: $count<br>";
var_dump(nl2br($str) );
$count ++ ;
}
?>
<?php
/* Test nl2br() function by passing
* single quoted strings containing various
* combinations of new line chars to 'str' argument
*/
echo "Testing nl2br() : usage variations.<br>";
$strings = array(
'\n',
'\r',
'\r\n',
'Hello\nWorld',
'Hello\rWorld',
'Hello\r\nWorld',
//one blank line
'
',
//two blank lines
'
',
//inserted new line
'Hello
World'
);
//loop through $strings array to test
// nl2br() function with each element
$count = 1;
foreach( $strings as $str ){
echo "<br><br>Iteration: $count<br>";
var_dump(nl2br($str) );
$count ++ ;
}
?>
<?php
/* Test nl2br() function by passing
* heredoc strings containing various
* combinations of new line chars to
* 'str' argument
*/
echo "Testing nl2br() : usage variations.<br>";
//heredoc string containing
// new line chars(\n, \r
// and combinations of \r & \n) and new lines
$heredoc_str1 = <<<EOD
\n
\r
\r\n
\nnn\n\n\nn
\rrr\r\r\rr
\n\r\n\r\r\n\nr\rn
EOD;
// heredoc string containing embedded
// 'new line chars'/'new lines' in the string
$heredoc_str2 = <<<EOD
Hello\nWorld\r
This is \tes\t for \n \new lines
like \n \r\n \r \n\r and etc
EOD;
var_dump(nl2br($heredoc_str1) );
echo '<br><br>';
var_dump(nl2br($heredoc_str2) );
?>
<?php
/*
* Test nl2br() function by passing html string
* inputs containing line breaks and
* new line chars for 'str'
*/
echo "Testing nl2br() : usage variations.<br>";
//array of html strings
$strings = [
"<html>Hello<br />world</html>",
"<html><br /></html>",
"<html>\nHello\r\nworld\r</html>",
"<html>\n \r\n \r</html>",
];
// loop through $strings array to test
// nl2br() function with each element
foreach( $strings as $str ){
var_dump(nl2br($str) );
echo '<br><br>';
}
?>