function.count-chars.html

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

HTML
165
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Return information about characters 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.convert-uuencode.html">convert_uuencode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.crc32.html">crc32</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.count-chars" class="refentry"> <div class="refnamediv">  <h1 class="refname">count_chars</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">count_chars</span> &mdash; <span class="dc-title">Return information about characters 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>count_chars</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">$mode</tt></span>  ] )</div>  <p class="para rdfs-comment">   Counts the number of occurrences of every byte-value (0..255) in   <i><tt class="parameter">string</tt></i> and returns it in various ways.  </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 examined string.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">mode</tt></i></span>     <dd>      <p class="para">       The optional parameter <i><tt class="parameter">mode</tt></i> defaults to 0.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   Depending on <i><tt class="parameter">mode</tt></i>   <b>count_chars()</b> returns one of the following:   <ul class="itemizedlist">    <li class="listitem">     <span class="simpara">      0 - an array with the byte-value as key and the frequency of      every byte as value.     </span>    </li>    <li class="listitem">     <span class="simpara">      1 - same as 0 but only byte-values with a frequency greater      than zero are listed.     </span>    </li>    <li class="listitem">     <span class="simpara">      2 - same as 0 but only byte-values with a frequency equal to      zero are listed.     </span>    </li>    <li class="listitem">     <span class="simpara">      3 - a string containing all unique characters is returned.     </span>    </li>    <li class="listitem">     <span class="simpara">      4 - a string containing all not used characters is returned.     </span>    </li>   </ul>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 <b>count_chars()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Two&nbsp;Ts&nbsp;and&nbsp;one&nbsp;F."</span><span style="color: #007700">;<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">count_chars</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"There&nbsp;were&nbsp;$val&nbsp;instance(s)&nbsp;of&nbsp;\""&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">)&nbsp;,&nbsp;</span><span style="color: #DD0000">"\"&nbsp;in&nbsp;the&nbsp;string.\n"</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>There were 4 instance(s) of &quot; &quot; in the string.There were 1 instance(s) of &quot;.&quot; in the string.There were 1 instance(s) of &quot;F&quot; in the string.There were 2 instance(s) of &quot;T&quot; in the string.There were 1 instance(s) of &quot;a&quot; in the string.There were 1 instance(s) of &quot;d&quot; in the string.There were 1 instance(s) of &quot;e&quot; in the string.There were 2 instance(s) of &quot;n&quot; in the string.There were 2 instance(s) of &quot;o&quot; in the string.There were 1 instance(s) of &quot;s&quot; in the string.There were 1 instance(s) of &quot;w&quot; in the string.</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.strpos.html" class="function" rel="rdfs-seeAlso">strpos()</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.convert-uuencode.html">convert_uuencode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.crc32.html">crc32</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 + -
显示快捷键?