<!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>Wraps a string to a given number of characters</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.vsprintf.html">vsprintf</a></div> <div class="next" style="text-align: right; float: right;"><a href="changelog.strings.html">Changelog</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.wordwrap" class="refentry"> <div class="refnamediv"> <h1 class="refname">wordwrap</h1> <p class="verinfo">(PHP 4 >= 4.0.2, PHP 5)</p><p class="refpurpose"><span class="refname">wordwrap</span> — <span class="dc-title">Wraps a string to a given number of characters</span></p> </div> <div class="refsect1 description" id="refsect1-function.wordwrap-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>wordwrap</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$width</code><span class="initializer"> = 75</span></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$break</code><span class="initializer"> = "\n"</span></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$cut</code><span class="initializer"> = false</span></span> ]]] )</div> <p class="para rdfs-comment"> Wraps a string to a given number of characters using a string break character. </p> </div> <div class="refsect1 parameters" id="refsect1-function.wordwrap-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">str</code></em></span> <dd> <p class="para"> The input string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">width</code></em></span> <dd> <p class="para"> The number of characters at which the string will be wrapped. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">break</code></em></span> <dd> <p class="para"> The line is broken using the optional <em><code class="parameter">break</code></em> parameter. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">cut</code></em></span> <dd> <p class="para"> If the <em><code class="parameter">cut</code></em> is set to <strong><code>TRUE</code></strong>, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example). </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.wordwrap-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the given string wrapped at the specified length. </p> </div> <div class="refsect1 changelog" id="refsect1-function.wordwrap-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>4.0.3</td> <td> The optional <em><code class="parameter">cut</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.wordwrap-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4929"> <p><strong>Example #1 <span class="function"><strong>wordwrap()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$text </span><span style="color: #007700">= </span><span style="color: #DD0000">"The quick brown fox jumped over the lazy dog."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$newtext </span><span style="color: #007700">= </span><span style="color: #0000BB">wordwrap</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #0000BB">20</span><span style="color: #007700">, </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$newtext</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> The quick brown fox<br /> jumped over the lazy<br /> dog. </pre></div> </div> </div> <div class="example" id="example-4930"> <p><strong>Example #2 <span class="function"><strong>wordwrap()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$text </span><span style="color: #007700">= </span><span style="color: #DD0000">"A very long woooooooooooord."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$newtext </span><span style="color: #007700">= </span><span style="color: #0000BB">wordwrap</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #0000BB">8</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$newtext</span><span style="color: #DD0000">\n"</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> A very long wooooooo ooooord. </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.wordwrap-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.nl2br.html" class="function" rel="rdfs-seeAlso">nl2br()</a> - Inserts HTML line breaks before all newlines in a string</span></li> <li class="member"> <span class="function"><a href="function.chunk-split.html" class="function" rel="rdfs-seeAlso">chunk_split()</a> - Split a string into smaller chunks</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.vsprintf.html">vsprintf</a></div> <div class="next" style="text-align: right; float: right;"><a href="changelog.strings.html">Changelog</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>