Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP count_chars() Function

PHP count_chars() Function

PHP String Reference Manual PHP String Reference Manual

Example

Return a string containing all the different characters used in "Hello World!" (mode 3):

Run Example »


Definition and Usage

The count_chars() function returns information about the characters used in a string (for example, how many times an ASCII character appears in the string, or whether a character has been used in the string).


Syntax

count_chars(

string,mode

)

Parameters Description
string Required. Specifies the string to check.
mode Optional. Specifies the return mode. Default is 0. The following different return modes are available:
  • 0 - Array, ASCII value as key, number of occurrences as value
  • 1 - Array, ASCII value as key, number of occurrences as value, listing only values that occur more than 0 times
  • 2 - Array, ASCII value as key, number of occurrences as value, listing only values that occur exactly 0 times
  • 3 - String, with all the different characters used
  • 4 - String, with all the different characters not used
  • |

    Technical Details

    Return Value: Depending on the specified mode parameter was added.
    PHP Version: 4+

    More examples

    Example 1

    Return a string containing all the characters not used in "Hello World!" (mode 4):

    Run Example »

    Example 2

    In this example, we will use count_chars() to inspect the string, with the return mode set to 1. Mode 1 returns an array with ASCII values as keys and occurrence counts as values:

    Run Example »

    Example 3

    Another example of counting how many times ASCII characters appear in a string:

    $value) { echo "The character '".chr($key)."' was found $value time(s)
    "; } ?>

    Run Example »


    PHP String Reference Manual PHP String Reference Manual