Jump to: navigation, search

Php count chars

From w3cyberlearnings

Contents

PHP function count_chars

This function checks how many times an ASCII character occurs within a string.

Syntax count_chars

  • string: string input
  • mode: return mode
    • 0 - array with the ASCII values as key and number of occurrences as value
    • 1 - only list the occurrence that is greater than zero. ASCII Value=> number of occurrences
    • 2 - only list the occurrence equals to zero. ASCII Value=>number of occurrences
    • 3 - List all characters in use in alphabetical order.
    • 4 - List with un-used characters.
count_chars(string);

Example 1

<?php

$str="gett";
print_r(count_chars($str,1));
?>

Output

Array
(
    [101] => 1
    [103] => 1
    [116] => 2
)

Example 2

<?php

$str="she is great";

$contains = count_chars($str,1);
foreach($contains as $k=>$v) {
	echo chr($k). ' contains '. $v. '<br/>';
}

?>


Output

contains 2
a contains 1
e contains 2
g contains 1
h contains 1
i contains 1
r contains 1
s contains 2
t contains 1

Example 3

<?php

$str="she";

print_r(count_chars($str,3));
?>

Output

ehs

Example 4:

<?php

$str="she a3133 ABCDEF";

print_r(count_chars($str,4));
?>

Output: Characters in $str is not displayed here

��������	
��
������������������!"#$%&'()*+,-./02456789:;
<=>?@GHIJKLMNOPQRSTUVWXYZ[\]^_`bcdfgijklmnopqr
tuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨
©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛ
ÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ

Related Functions


Navigation
Web
SQL
MISC
References