Thursday, 25 September 2014

count_chars in PHP

PHP count_chars() function counts the number of times an ASCII character occurs within a string and returns the information.
OR
"PHP count_chars() function counts the number character occurs within a string and returns the information".

Syntax:

count_chars(string,mode)
 
Parameters Description: 
string : Required. Defines the input string.
mode : Optional. Defines the return values. Default is 0.

The different return modes are:

  • 0 - an array with the byte-value as key and the frequency of each byte as value.
  • 1 - same as 0 however just byte-values with a frequency greater than zero are listed
  • 2 - same as 0 however just byte-values with a frequency equivalent to zero are listed.
  • 3 - a string holding all unique characters is returned.
  • 4 - a string holding all not utilized characters is returned.

Example:

<?php
echo "<strong>here we will use count_chars() with mode 1 to check the string. Mode 1 will return an array with the ASCII value as key and how many times it occurred as value :</strong><br />";
$str_name = "Good Morning!";
print_r(count_chars($str_name,1));
echo "<br /><strong> now we will use the count_chars() with mode 3 to check the string. Mode 3 will return a string with all the different characters used :</strong><br />";
echo count_chars($str_name,3);
?>

O/P:

here we will use count_chars() with mode 1 to check the string. Mode 1 will return an array with the ASCII value as key and how many times it occurred as value :

Array ( 
          [32] => 1 
          [33] => 1
          [71] => 1 
          [77] => 1 
          [100] => 1 
          [103] => 1
          [105] => 1
          [110] => 2 
          [111] => 3 
          [114] => 1
        )
now we will use the count_chars() with mode 3 to check the string. Mode 3 will return a string with all the different characters used :

!GMdginor

0 comments:

Post a Comment