What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.



count_chars — Gets information about the characters used in a string.


mixed count_chars(string[, mode]);
string string: String in which to count characters
int mode (optional): Flag specifying what information should be returned


Array or string containing character information; FALSE if the mode parameter is less than 0 or greater than 4


count_chars() counts the number of times that an ASCII character occurs within a string and returns the information in one of five possible ways.

The optional parameter mode defaults to0. Depending on mode , count_chars() returns one of the following:

  • 0: An array with the byte value as key and the frequency of every byte as value

  • 1: Like 0, but only byte values with a frequency greater than zero are listed

  • 2: Like 0, but only byte values with a frequency equal to zero are listed

  • 3: Returns a string containing all used byte values

  • 4: Returns a string containing all unused byte values

The following program displays the hex value of the character, followed by the character in brackets and one dot for every time the character is used.


PHP 4.0b4+

See also

Other functions that deal with characters:








Example 1196. Display a histogram of the frequency with which characters occur in a string

$text = <<<_ANTONY_
Friends, Romans, countrymen, lend me your ears;
I come to bury Caesar, not to praise him.
The evil that men do lives after them;
The good is oft interred with their bones:

foreach(count_chars($text, 1) as $ord => $amount) {
   $chr = chr($ord);

   // Convert whitepace characters to a single space - doing so
   // prevents the character from breaking the histogram formatting
   $chr = ereg_replace('[[:space:]]', ' ', $chr);

   printf("%02X (%s) %'.".$amount."s\n", $ord, $chr, '');

0A ( ) ...
20 ( ) ............................
2C (,) ....
2E (.) .
3A (:) .
3B (;) ..
43 (C) .
46 (F) .
49 (I) .
52 (R) .
54 (T) ..
61 (a) .......
62 (b) ..
63 (c) ..
64 (d) .....
65 (e) ...................
66 (f) ..
67 (g) .
68 (h) .......
69 (i) .........
6C (l) ...
6D (m) .......
6E (n) .........
6F (o) ............
70 (p) .
72 (r) ...........
73 (s) ........
74 (t) ............
75 (u) ...
76 (v) ..
77 (w) .
79 (y) ...

PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at The authors of this book have elected not to choose any options under the OPL. This online book was obtained from and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.