function.str-word-count.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 242 行

HTML
242
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Return information about words used in a string</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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 &gt;= 4.3.0, PHP 5)</p><p class="refpurpose"><span class="refname">str_word_count</span> &mdash; <span class="dc-title">   Return information about words used in a string  </span></p> </div> <div class="refsect1 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"><b><b>str_word_count</b></b></span>    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$string</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$format</tt></span>   [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$charlist</tt></span>  ]] )</div>  <p class="para rdfs-comment">   Counts the number of words inside <i><tt class="parameter">string</tt></i>.    If the optional <i><tt class="parameter">format</tt></i> is not specified, then    the return value will be an integer representing the number of words    found. In the event the <i><tt class="parameter">format</tt></i> is specified, the return   value will be an array, content of which is dependent on the    <i><tt class="parameter">format</tt></i>. The possible value for the    <i><tt class="parameter">format</tt></i> and the resultant outputs are listed below.  </p>  <p class="para">   For the purpose of this function, &#039;word&#039; is defined as a locale dependent   string containing alphabetic characters, which also may contain, but not start   with &quot;&#039;&quot; and &quot;-&quot; characters.  </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">string</tt></i></span>     <dd>      <p class="para">       The string      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">format</tt></i></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          <i><tt class="parameter">string</tt></i>         </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 <i><tt class="parameter">string</tt></i> and          the value is the actual word itself         </span>        </li>        </ul>      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">charlist</tt></i></span>     <dd>      <p class="para">       A list of additional characters which will be considered as &#039;word&#039;      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   Returns an array or an integer, depending on the   <i><tt class="parameter">format</tt></i> chosen.  </p> </div> <div class="refsect1 changelog">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">5.1.0</td>       <td colspan="1" rowspan="1" align="left">        Added the <i><tt class="parameter">charlist</tt></i> parameter       </td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 A <b>str_word_count()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;fri3nd,&nbsp;you're<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;looking&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;good&nbsp;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">,&nbsp;</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">,&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'脿谩茫莽3'</span><span style="color: #007700">));<br /><br />echo&nbsp;</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">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>Array(    [0] =&gt; Hello    [1] =&gt; fri    [2] =&gt; nd    [3] =&gt; you&#039;re    [4] =&gt; looking    [5] =&gt; good    [6] =&gt; today)Array(    [0] =&gt; Hello    [6] =&gt; fri    [10] =&gt; nd    [14] =&gt; you&#039;re    [29] =&gt; looking    [46] =&gt; good    [51] =&gt; today)Array(    [0] =&gt; Hello    [1] =&gt; fri3nd    [2] =&gt; you&#039;re    [3] =&gt; looking    [4] =&gt; good    [5] =&gt; today)7</pre></div>    </pre></div>   </div>  </p> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.explode.html" class="function" rel="rdfs-seeAlso">explode()</a></li>    <li class="member"><a href="function.preg-split.html" class="function" rel="rdfs-seeAlso">preg_split()</a></li>    <li class="member"><a href="function.split.html" class="function" rel="rdfs-seeAlso">split()</a></li>    <li class="member"><a href="function.count-chars.html" class="function" rel="rdfs-seeAlso">count_chars()</a></li>    <li class="member"><a href="function.substr-count.html" class="function" rel="rdfs-seeAlso">substr_count()</a></li>   </ul>  </p> </div></div><hr /><div 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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?