PHP count_chars: How to Count ASCII Characters of String

0
8
PHP count_chars() Function Example Tutorial

The count_chars() function in PHP is used to perform several operations related to string, like a number of an ASCII character occurs in the string.

PHP count_chars()

PHP count_chars() is a built-in function that returns information about characters used in the string. For example, 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.

Syntax

See the following syntax of the function.

count_chars(string, return_mode);

Parameters

  1. string: The string parameter refers to an input string on which the operation is 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. The corresponding values will be several 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 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
)

That’s it for this example.

Recommended Posts

  1. PHP header()
  2. PHP Regular Expression
  3. PHP empty()
  4. PHP isset()
  5. PHP chop()

Leave A Reply

Please enter your comment!
Please enter your name here

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