count_charsReturnsArray or string containing character information; FALSE if the mode parameter is less than 0 or greater than 4 Descriptioncount_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:
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. ExampleExample 1196. Display a histogram of the frequency with which characters occur in a string <?php $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: _ANTONY_; 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, ''); } ?> Output: 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
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
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.
|