<!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 characters 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.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> — <span class="dc-title">Return information about characters used in a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.count-chars-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>count_chars</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">$mode</code><span class="initializer"> = 0</span></span> ] )</div> <p class="para rdfs-comment"> Counts the number of occurrences of every byte-value (0..255) in <em><code class="parameter">string</code></em> and returns it in various ways. </p> </div> <div class="refsect1 parameters" id="refsect1-function.count-chars-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 examined string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">mode</code></em></span> <dd> <p class="para"> See return values. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.count-chars-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Depending on <em><code class="parameter">mode</code></em> <span class="function"><strong>count_chars()</strong></span> 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" id="refsect1-function.count-chars-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4822"> <p><strong>Example #1 <span class="function"><strong>count_chars()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$data </span><span style="color: #007700">= </span><span style="color: #DD0000">"Two Ts and one F."</span><span style="color: #007700">;<br /><br />foreach (</span><span style="color: #0000BB">count_chars</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">) as </span><span style="color: #0000BB">$i </span><span style="color: #007700">=> </span><span style="color: #0000BB">$val</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"There were </span><span style="color: #0000BB">$val</span><span style="color: #DD0000"> instance(s) of \"" </span><span style="color: #007700">, </span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">) , </span><span style="color: #DD0000">"\" in the string.\n"</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> There were 4 instance(s) of " " in the string. There were 1 instance(s) of "." in the string. There were 1 instance(s) of "F" in the string. There were 2 instance(s) of "T" in the string. There were 1 instance(s) of "a" in the string. There were 1 instance(s) of "d" in the string. There were 1 instance(s) of "e" in the string. There were 2 instance(s) of "n" in the string. There were 2 instance(s) of "o" in the string. There were 1 instance(s) of "s" in the string. There were 1 instance(s) of "w" in the string. </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.count-chars-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.strpos.html" class="function" rel="rdfs-seeAlso">strpos()</a> - Find the position of the first occurrence of a substring 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.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>