PHP nl2br() Function
Example
Insert line breaks where newlines (\n) occur in the string:
<?php
echo nl2br("One line.\nAnother line.");
?>
The browser output of the code above will be:
One line.
Another line.
The HTML output of the code above will be (View Source):
One line.<br />
Another line.
Definition and Usage
The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.
Syntax
nl2br(string,xhtml)
Parameter Description
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 <br />
FALSE - Inserts <br>
Technical Details
Return Value: Returns the converted string
PHP Version: 4+
Changelog: Before PHP 4.0.5, this function inserted <br>. After PHP 4.0.5 it inserts the XHTML compliant <br />
The xhtml parameter was added in PHP 5.3.
More Examples
Example 1
Insert line breaks where newlines (\n) occur, using the xhtml parameter:
<?php
echo nl2br("One line.\nAnother line.",false);
?>
The browser output of the code above will be:
One line.
Another line.
The HTML output of the code above will be (View Source):
One line.<br>
Another line.
___________________________________________________
<!DOCTYPE html>
<html>
<body>
<?php
echo nl2br("One line.\nAnother line.");
?>
</body>
</html>
***********
One line.
Another line.
___________________________________________________
<!DOCTYPE html>
<html>
<body>
<?php
echo nl2br("One line.\nAnother line.",false);
?>
</body>
</html>
***********
One line.
Another line.
___________________________________________________
whats the difference between <br> and <br />
The first is used in HTML, the second is used in XHTML. They are not compatible (you must use the first in HTML only, and the second in XHTML only).
For a more technical answer, XHTML requires all elements [edit: without closing tags] to be "self-closed" (it's in the rules). Adding a space and a forward slash before the end of the tag "self-closes" the element, in this case <br> becomes <br /> which is technically the same as <br></br>
(Although, to be really REALLY technical, the space is not even required, it just helps make the tag easier to read by us mere puny humans with our flawed primate vision.)
The self-closing "rule" in XHTML only applies to elements that do not have corresponding closing tags. (like IMG, BR, META and so on)
IE 7 doesn't support true XHTML either. You have to "fake it" by sending it as text/html instead.
___________________________________________________
<!DOCTYPE html>
<html>
<body>
<?php
echo nl2br("nl2br(\"line1 [press enter] line2\"); will enter new line2 ; example:
line1
line2");
echo nl2br("\n"); // nl2br("\n"); out: new line
$new_line = "nl2br(\"\\n\"); out: new line. \\n is next new line code for php, must put echo nl2br(\" in Left \"); in Right";
$note = "// is for note only will not run";
$xjx = "\\ is for out put special character, example \\\" will out \" , \\\\n out \\n";
echo nl2br("$new_line \n\n $note \n $xjx \n . is add + ; examble a.b out ab \n");
?>
</body>
</html>
***********
nl2br("line1 [press enter] line2"); will enter new line2 ; example:
line1
line2
nl2br("\n"); out: new line. \n is next new line code for php, must put echo nl2br(" in Left "); in Right
// is for note only will not run
\ is for out put special character, example \" will out " , \\n out \n
. is add + ; examble a.b out ab
___________________________________________________
white space in PHP , USING str_repeat
<?php
echo "a".str_repeat(' ', 5)."b"; // adds 5 spaces ; out a b
?>
___________________________________________________
echo "<p>Hello World"; // = 1 white space
// out: Hello World (with 5 white spaces)
echo "<p> Hello World"; // This will render as Hello World (with only one space)
***********
Hello World
Hello World
___________________________________________________
For showing data in raw format (with exact number of spaces and "enters") use HTML <pre> tag.
[ the bad side is the character output is skinny small font size ]
echo "<pre>Hello World</pre>"; //Will render exactly as written here (8 white spaces)
echo "<pre>Hello
World</pre>";
echo "<h2><pre>Hello World</pre></h2>"; //Will incress the font size
***********
Hello World
Hello
World
Hello World [Bigger Font Size]
___________________________________________________
<?php
echo "hello";
echo "world"; // will join the line above, no spacing
echo "<p>hello</p>"; // add one new blank line at top
echo "<p>world</p>";
echo "
<div>
easy formatting<br />
across multiple lines!<br />
using div + br /
</div>
";
?>
***********
helloworld
hello
world {will see a new blank line at web site , but actually no new line}
easy formatting
across multiple lines!
using div + br /
___________________________________________________
\x20 = white space
{ useless, just the same function like space , useless }
___________________________________________________
referance
http://adf.ly/1hATtE
http://adf.ly/1hATvA
http://adf.ly/1hAU0M
http://adf.ly/1hAU3M
http://adf.ly/1hAU6Z

No comments:
Post a Comment
Note: only a member of this blog may post a comment.