The count_chars() is an inbuilt function in PHP and is used to perform several operations related to string like the number of an ASCII character occurs in a string.
Syntax :
count_chars(string,return_mode);
Parameters: The count_chars() function takes two parameters string and return_mode as explained below:
- string : This parameter refers to the input string on which the operation is to be performed.
-
return_mode : This parameter is optional. This parameter defines the operation needed to be performed on the string. It takes value 0, 1, 2, 3, 4.
- 0 : If this mode is chosen, the function will return an array with key-value pairs whose keys are ASCII values and the corresponding values will be the number of occurrences of that ASCII value.
- 1 : If this mode is chosen, the count_chars() function will return an array with key-value pairs whose keys are ASCII values and the corresponding values will be the number of occurrences of that ASCII value . Here, the array will contain only those keys as ASCII values whose frequency is greater than 0.
- 2 : In this mode, the function will return an array of key-value pairs where key are the ASCII value whose frequency in the string is 0.
- 3 : In this mode the count_chars() function will return a string of all different characters used in the string in ascending order.
- 4 : In this mode the count_chars() function will return a string of characters that are not used in input string
Return Type: This function will return an array or string depending on the parameter return_mode as described above.
Examples:
Input : string = "GeeksforGeeks" , return_mode = 3 Output : Gefkors
Below is the PHP program to illustrate the working of count_chars() function:
<?php // PHP program to illustrate count_chars() // Input string $string = "geeksforgeeks"; // return_mode 1 print_r(count_chars($string,1)); // return_mode 3 print_r(count_chars($string,3)); // return_mode 4 print_r(count_chars($string,4)); ?> |
Output:
Array
(
[101] => 4
[102] => 1
[103] => 2
[107] => 2
[111] => 1
[114] => 1
[115] => 2
)
efgkors
!"#$%&'()*+,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXY
Z[\]^_`abcdhijlmnpqtuvwxyz{|}~??????????????????????
????? ¡¢£¤¥¦§¨©ª«¬®¯´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×
ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
The above program shows the return values for string “geeksforgeeks” with return_mode as 1, 3 and 4. You can modify the program by changing the value of return_mode in the function call to see the returned values for modes 0 and 2 also.
Recommended Posts:
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- PHP | Ds\Set add() Function
- D3.js | d3.set.add() Function
- PHP | Ds\Map get() Function
- PHP | next() Function
- PHP | each() Function
- p5.js | nfs() Function
- PHP | Ds\Set last() Function
- PHP | min( ) Function
- p5.js | nf() Function
- p5.js | nfc() function
- p5.js | arc() Function
- PHP | key() Function
- p5.js | box() Function
- D3.js | d3.map.set() Function
- PHP | pos() Function
- p5.js | nfp() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh

