count_chars 


string apg

RETURN the information about number of characters used in a STRING.





This function counts the number of occurrences of every byte-value from 0 to 255, in string and returns it in various ways.



<?php

mix count_chars 
str $string [, int $mode ] )


where,

$string The string to be examined

$mode 
To control the format of returned values
              
SEE the below TABLE )

?> 

$string


The input STRING to be examined.



$mode


To control how the STRING will be examined.

VALUE WHAT RETURN
0 an array with the byte-value as key and the
frequency of every byte as value
1 same as 0 but only byte-values with a
frequency greater than zero are listed
2 same as 0 but only byte-values with a
frequency equal to zero are listed
3 a string containing all unique
characters is returned
4 a string containing all not used
characters is returned
ed48


  1 EXERCISE   

<?php

$txta 
"The quick brown fox jumps over the lazy dog.";

echo 
$txta;

echo 
'<br><br><br>';


$itblei '<div class="circ bfff">
      <table width="100%" cellspacing="5" cellpadding="5" 
                        border="1" align="center">
<tbody>'
;

$tit0 '<tr><td colspan="3">ALL ( MODE 0 )</td></tr>';
$tit1 '<tr>
           <td colspan="3">EFFECTIVE OCCURRENCES ( MODE 1 )
                                   </td></tr>'
;
$tit2 '<tr><td colspan="3">NOT USED ( MODE 2 )</td></tr>';
$tit3 'EFFECTIVE OCCURRENCES ( MODE 3 )';
$tit4 'NOT USED ( MODE 4 )';

$itblee '<tr><td width="33%">ASCII #</td>
<td width="33%">CHARACTER</td>
<td width="34%"># REPETITIONS</td></tr>'
;

$etble '<tr>
        <td colspan="3">ed48</td></tr>
                   </tbody></table></div><br>'
;

// mode = 0
// ARRAY -> ASCII CHARACTERS ε[ 0, 255 ] - ALL
$count0 count_chars($txta);

echo 
$itblei $tit0 $itblee;
foreach(
$count0 as $cnt0 => $cn0)
{
echo 
'<tr><td>' $cnt0 '</td><td>' 
            
chr($cnt0) . '</td><td>' $cn0 
                        
'</td></tr>';
}
echo 
$etble '<br>';

// mode = 1
// ARRAY -> ASCII CHARACTERS ε[ 0, 255 ]
// - EFFECTIVE OCCURRENCES
$count1 count_chars($txta1);

echo 
$itblei $tit1 $itblee;
foreach(
$count1 as $cnt1 => $cn1)
{
echo 
'<tr><td>' $cnt1 '</td><td>' 
           
chr($cnt1) . '</td><td>' $cn1 
                      
'</td></tr>';
}
echo 
$etble '<br>';

// mode = 2
// ARRAY -> ASCII CHARACTERS ε[ 0, 255 ]
// - NOT USED
$count2 count_chars($txta2);

echo 
$itblei $tit2 $itblee;
foreach(
$count2 as $cnt2 => $cn2)
{
echo 
'<tr><td>' $cnt2 '</td><td>' 
              
chr($cnt2) . '</td><td>' $cn2 
                                     
'</td></tr>';
}
echo 
$etble '<br>';


// mode = 3
// STRING -> ASCII CHARACTERS ε[ 0, 255 ]
// - EFFECTIVE OCCURRENCES
$count3 count_chars($txta3);

echo 
$tit3 '<br>' $count3 '<br><br>';

// mode = 4
// STRING -> ASCII CHARACTERS ε[ 0, 255 ]
// - NOT USED
$count4 count_chars($txta4);
echo 
$tit4 '<br>' $count4;

?>

  2 EXERCISE   

<?php

echo "Testing count_chars() : basic functionality.<br><br>";

$string "Return information about 
             characters used in a string."
;

echo 
"<br><br>$string<br><br>";

var_dump(count_chars($string));
echo 
'<br><br>';
var_dump(count_chars($string0));
echo 
'<br><br>';
var_dump(count_chars($string1));
echo 
'<br><br>';
var_dump(count_chars($string2));
echo 
'<br><br>';
var_dump(count_chars($string3));
echo 
'<br><br>';
var_dump(count_chars($string4));

try {
    
count_chars($string5);
} catch (
ValueError $e) {
    echo 
$e->getMessage(), "<br>";
}

?>