<!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>Return information about words used 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.str-split.html">str_split</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strcasecmp.html">strcasecmp</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.str-word-count" class="refentry"> <div class="refnamediv"> <h1 class="refname">str_word_count</h1> <p class="verinfo">(PHP 4 >= 4.3.0, PHP 5)</p><p class="refpurpose"><span class="refname">str_word_count</span> — <span class="dc-title"> Return information about words used in a string </span></p> </div> <div class="refsect1 description" id="refsect1-function.str-word-count-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>str_word_count</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$format</code><span class="initializer"> = 0</span></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$charlist</code></span> ]] )</div> <p class="para rdfs-comment"> Counts the number of words inside <em><code class="parameter">string</code></em>. If the optional <em><code class="parameter">format</code></em> is not specified, then the return value will be an integer representing the number of words found. In the event the <em><code class="parameter">format</code></em> is specified, the return value will be an array, content of which is dependent on the <em><code class="parameter">format</code></em>. The possible value for the <em><code class="parameter">format</code></em> and the resultant outputs are listed below. </p> <p class="para"> For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with "'" and "-" characters. </p> </div> <div class="refsect1 parameters" id="refsect1-function.str-word-count-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 string </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">format</code></em></span> <dd> <p class="para"> Specify the return value of this function. The current supported values are: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> 0 - returns the number of words found </span> </li> <li class="listitem"> <span class="simpara"> 1 - returns an array containing all the words found inside the <em><code class="parameter">string</code></em> </span> </li> <li class="listitem"> <span class="simpara"> 2 - returns an associative array, where the key is the numeric position of the word inside the <em><code class="parameter">string</code></em> and the value is the actual word itself </span> </li> </ul> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">charlist</code></em></span> <dd> <p class="para"> A list of additional characters which will be considered as 'word' </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.str-word-count-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array or an integer, depending on the <em><code class="parameter">format</code></em> chosen. </p> </div> <div class="refsect1 changelog" id="refsect1-function.str-word-count-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.1.0</td> <td> Added the <em><code class="parameter">charlist</code></em> parameter </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.str-word-count-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4883"> <p><strong>Example #1 A <span class="function"><strong>str_word_count()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"Hello fri3nd, you're<br /> looking good today!"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">str_word_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">str_word_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">str_word_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'àáãç3'</span><span style="color: #007700">));<br /><br />echo </span><span style="color: #0000BB">str_word_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /><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> Array ( [0] => Hello [1] => fri [2] => nd [3] => you're [4] => looking [5] => good [6] => today ) Array ( [0] => Hello [6] => fri [10] => nd [14] => you're [29] => looking [46] => good [51] => today ) Array ( [0] => Hello [1] => fri3nd [2] => you're [3] => looking [4] => good [5] => today ) 7 </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.str-word-count-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.explode.html" class="function" rel="rdfs-seeAlso">explode()</a> - Split a string by string</span></li> <li class="member"> <span class="function"><a href="function.preg-split.html" class="function" rel="rdfs-seeAlso">preg_split()</a> - Split string by a regular expression</span></li> <li class="member"> <span class="function"><a href="function.split.html" class="function" rel="rdfs-seeAlso">split()</a> - Split string into array by regular expression</span></li> <li class="member"> <span class="function"><a href="function.count-chars.html" class="function" rel="rdfs-seeAlso">count_chars()</a> - Return information about characters used in a string</span></li> <li class="member"> <span class="function"><a href="function.substr-count.html" class="function" rel="rdfs-seeAlso">substr_count()</a> - Count the number of substring occurrences</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.str-split.html">str_split</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strcasecmp.html">strcasecmp</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>