CancelImage Upload

How to count the number of appearances of a letter inside a string with PHP

To count the number of appearances of each character within a string we can use the following PHP function:

count_chars($string, $mode);

The function returns an array loaded with the individual characters and the number of their appearances. The $mode variable defines the values to be returned.

0 = Array with ASCII values as the key
1 = Only returns the characters which make an appearance inside the string
2 = Characters that do not make an appearance
3 = Returns a string with all available ASCII codes
4 = Returns a string with all ASCII codes that were not used

Here then is an example of how to return the number of times each present character was used:

<?php

$string = "This is a string with a number of chars";

$charsCount = count_chars($string, 1);

while(list($key, $value) = each($charsCount))
{

      echo "<b>&#".$key."; </b>: ".$value."<br>";

}

?>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com