Friday, April 17, 2009

The count_chars() function

This function can be used to determine the characters in a string and the number of times they are used.

syntax: count_chars(string,mode);

string – the input string
mode

This parameter will determine the output of the count_chars() function.

Following are the possible values and implications of this parameter.
0 – The function returns an array with the ASCII value of every possible character as the key and the number of occurrances in the string as the value.

1 – The function returns anarray with the ASCII values as keys and the number of occurrances as values. In this case, the function creates an array element for characters that occur atleast once in the string.

2 - The function returns anarray with the ASCII values as keys and the number of occurrances as values. The array elements will correspond to characters that are not present in the string.

3 – The function will just display all the characters that are present in the input string.

4 – The function displays all the characters that are not present in the input string.


Example input:

$str = "Rooney";
echo $str;
echo "
";
print_r(count_chars($str,1));
echo "
";
echo count_chars($str,3);
echo "
";
echo count_chars($str,4);

Example output:

Rooney

Array ( [82] => 1 [101] => 1 [110] => 1 [111] => 2 [121] => 1 )

Renoy
******

No comments: