AppDividend
Latest Code Tutorials

PHP count_chars() Function Example Tutorial

0

PHP count_chars() Function Example Tutorial is today’s topic. The count_chars() function returns an information about characters used in the string. It counts how many times the ASCII character occurs in the string, or which character has been used or not been used in the string. The count_chars() is the inbuilt function in PHP and is used to perform the several operations related to string like a number of an ASCII character occurs in the string.

PHP count_chars()

See the following syntax of the function.

count_chars(string, return_mode);
  1. string: The string parameter refers to an input string on which the operation is to be performed.
  2. return_mode: The return_mode parameter is optional. The parameter defines an operation needed to be performed on the string. It takes value 0, 1, 2, 3, 4.
    1. 0: If this mode is chosen, the function will return the array with key-value pairs whose keys are ASCII values, and the corresponding values will be a number of occurrences of that ASCII value.
    2. 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, an array will contain only those keys as ASCII values whose frequency is greater than 0.
    3. 2: In this mode, the function will return the array of key-value pairs where the key is the ASCII value whose frequency in the string is 0.
    4. 3: In this mode, the function will return a string of all different characters used in the string in ascending order.
    5. 4: In this mode, the cfunction will return a string of characters that are not used in the input string

See the following code example.

<?php

$model = "MillieBobbyBrown!";
print_r(count_chars($model, 1));

See the output.

➜  pro php app.php
Array
(
    [33] => 1
    [66] => 2
    [77] => 1
    [98] => 2
    [101] => 1
    [105] => 2
    [108] => 2
    [110] => 1
    [111] => 2
    [114] => 1
    [119] => 1
    [121] => 1
)
➜  pro

See another example of counting how many times an ASCII character occurs in a string.

<?php

$str = "Friends Dont Lie!!";
$strArray = count_chars($str, 1);

foreach ($strArray as $key=>$value)
{
  echo "The character'".chr($key)."' was found $value time(s)". "\n";
}

See the output.

➜  pro php app.php
The character' ' was found 2 time(s)
The character'!' was found 2 time(s)
The character'D' was found 1 time(s)
The character'F' was found 1 time(s)
The character'L' was found 1 time(s)
The character'd' was found 1 time(s)
The character'e' was found 2 time(s)
The character'i' was found 2 time(s)
The character'n' was found 2 time(s)
The character'o' was found 1 time(s)
The character'r' was found 1 time(s)
The character's' was found 1 time(s)
The character't' was found 1 time(s)
➜  pro

See 1, 3, and 4 modes as a second argument in the following code.

<?php

$string = "AppDividend"; 

// 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));

See the output.

➜  pro php app.php
Array
(
    [65] => 1
    [68] => 1
    [100] => 2
    [101] => 1
    [105] => 2
    [110] => 1
    [112] => 2
    [118] => 1
)
ADdeinpv


 !"#$%&'()*+,-./0123456789:;<=>?@BCEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcfghjklmoqrstuwxyz{|}~��������������������������������������������������������������������������������������������������������������������������������                        
➜  pro

Finally, PHP count_chars() Function Example Tutorial is over.

Recommended Posts

  1. PHP header() Function Example | header() Method In PHP Tutorial
  2. PHP Regular Expression Example | Regular Expressions in PHP Tutorial
  3. PHP empty Example | empty() Function In PHP Tutorial
  4. PHP isset Example | PHP isset() Function Tutorial
  5. PHP chop() Function Example | PHP String chop() Method

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.