<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Inserts HTML line breaks before all newlines in a string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.nl-langinfo.html">nl_langinfo</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.number-format.html">number_format</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.nl2br" class="refentry"> <div class="refnamediv"> <h1 class="refname">nl2br</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">nl2br</span> — <span class="dc-title">Inserts HTML line breaks before all newlines in a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.nl2br-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>nl2br</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$is_xhtml</code><span class="initializer"> = true</span></span> ] )</div> <p class="para rdfs-comment"> Returns <em><code class="parameter">string</code></em> with '<br />' or '<br>' inserted before all newlines (<em>\r\n</em>, <em>\n\r</em>, <em>\n</em> and <em>\r</em>). </p> </div> <div class="refsect1 parameters" id="refsect1-function.nl2br-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">string</code></em></span> <dd> <p class="para"> The input string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">is_xhtml</code></em></span> <dd> <p class="para"> Whether to use XHTML compatible line breaks or not. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.nl2br-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the altered string. </p> </div> <div class="refsect1 examples" id="refsect1-function.nl2br-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4850"> <p><strong>Example #1 Using <span class="function"><strong>nl2br()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">nl2br</span><span style="color: #007700">(</span><span style="color: #DD0000">"foo isn't\n bar"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> foo isn't<br /> bar </pre></div> </div> </div> <div class="example" id="example-4851"> <p><strong>Example #2 Generating valid HTML markup using the <em><code class="parameter">is_xhtml</code></em> parameter</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">nl2br</span><span style="color: #007700">(</span><span style="color: #DD0000">"Welcome\r\nThis is my HTML document"</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Welcome<br> This is my HTML document </pre></div> </div> </div> <div class="example" id="example-4852"> <p><strong>Example #3 Various newline separators</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$string </span><span style="color: #007700">= </span><span style="color: #DD0000">"This\r\nis\n\ra\nstring\r"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">nl2br</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> This<br /> is<br /> a<br /> string<br /> </pre></div> </div> </div> </p> </div> <div class="refsect1 changelog" id="refsect1-function.nl2br-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> Added the optional <em><code class="parameter">is_xhtml</code></em> parameter. </td> </tr> <tr> <td>4.0.5</td> <td> <span class="function"><strong>nl2br()</strong></span> is now XHTML compliant. All older versions will return <em><code class="parameter">string</code></em> with '<br>' inserted before newlines instead of '<br />'. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 seealso" id="refsect1-function.nl2br-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.htmlspecialchars.html" class="function" rel="rdfs-seeAlso">htmlspecialchars()</a> - Convert special characters to HTML entities</span></li> <li class="member"> <span class="function"><a href="function.htmlentities.html" class="function" rel="rdfs-seeAlso">htmlentities()</a> - Convert all applicable characters to HTML entities</span></li> <li class="member"> <span class="function"><a href="function.wordwrap.html" class="function" rel="rdfs-seeAlso">wordwrap()</a> - Wraps a string to a given number of characters</span></li> <li class="member"> <span class="function"><a href="function.str-replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a> - Replace all occurrences of the search string with the replacement string</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.nl-langinfo.html">nl_langinfo</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.number-format.html">number_format</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>