Count Characters and Words

Get byte values of all characters in a string and find how many times the byte-value occurred, how many times a character appears in a given string, receives unique characters from a string, etc. with count_chars() function. Also, count words in a string, convert a string into an array by words, get the frequency of all words in a string, find the position of each word in a string, etc. with str_​word_​count() function.

  1. count_chars()
  2. str_​word_​count()

Get information about characters used in a string

 count_chars(string $string, int $mode = 0): array|string
  1. $string: the input string that you want to examine.
  2. $mode (optional): an integer value from 0 to 4, default value is 0.

The count_chars() function returns information about the characters found in a string. For example, you can get unique characters and byte values of each character in the given string based on the $mode value:

Example: count_chars with mode value is 0 (default)

 $text = count_chars('', 0);

The above example prints the returned array containing all used and unused bytes in the string. In the following figure, we highlighted the used elements in red to distinguish them from unused elements. From the following output, the element “[46]=>1“, 46 is the byte-value and 1 is frequency, which means the byte-value 46 (.) appeared only once in the string. You can use the chr() function to convert a byte value into its corresponding character.

Example: get byte values and number of occurrences of each character in a string

If you assign value 1 to $mode parameter, the count_chars() function will returns an array with the byte value as its keys and the number of occurrences of every byte (if not zero) as its values, see example:

 $a = count_chars('', 1);
Array ( [46] => 1 [66] => 2 [97] => 1 [99] => 1 [101] => 1 [105] => 1 [108] => 2 [109] => 1 [110] => 1 [111] => 1 [114] => 1 )

Example: how many times a character appeared in a given string

The following example displays how many times a character appeared in the string in human-readable form:

 $a = count_chars('', 1);
 foreach($a as $k=>$v)
  echo chr($k)." appears $v times<br>\n";
. appears 1 times
B appears 2 times
a appears 1 times
c appears 1 times
e appears 1 times
i appears 1 times
l appears 2 times
m appears 1 times
n appears 1 times
o appears 1 times
r appears 1 times

Example: get unique characters in a given string

If you assign value 3 to $mode parameter, the count_chars() function will return a string containing all unique characters in the given string. See example:

 echo count_chars('', 3);
 #Prints: .Baceilmnor


Words are defined as a string of alphabetical characters, depending on the locale settings, and may contain but not start with - and '.

 str_word_count(string $string, int $format = 0, ?string $characters = null): array|int

The str_word_count() function has three parameters:

  1. $string: the string to examine.
  2. $format (optional): an integer value 0, 1 or 2, default is 0.
  3. $characters (optional): additional characters which will be considered as ‘word’.

By default, the str_word_count() function returns the total number of words found in a string. You can change the default behavior by defining the $format parameter:

  • 0 – (default) returns total number of words.
  • 1 – returns an array containing all the words found inside the string.
  • 2 – returns an associative array, where the key is the numeric position of the word in the string and the value is the word itself.

Example: Count words in a given string

The str_word_count() function provides an easy way to count the number of words in a string. See example:

 echo str_word_count('');
 //Prints: 4

Example: Converting string to array by word

 $array = str_word_count('', 1);
    [0] => BrainBell
    [1] => com
    [2] => BrainBell
    [3] => com

Example: Get the frequency of all words in a given string

Use the str_word_count() and array_count_values() functions to determine the frequency in which each word appears in an array.

 $a = str_word_count('My website:', 1);
 $b = array_count_values($a);
    [My] => 1
    [website] => 1
    [BrainBell] => 2
    [com] => 2

Example: Find the position of each word in a given string

Define $format value 2 to get the numeric position of each word.

 $a = str_word_count('My website:', 2);
    [0] => My
    [3] => website
    [12] => BrainBell
    [22] => com
    [26] => BrainBell
    [36] => com

Follow this tutorial if you want to find positions of a substring in a string.

Example: add characters that will be accepted as part of a word

We know that words are alphabetical characters and may contain but not start with - and '. So, if a word contains a number or a non-alphabet character, that single word will breaks into two words, in the following example the string breaks into four words:

 $str = 'H3llö Wörld';
 echo str_word_count($str); //4
 $a = str_word_count($str, 1);
 /* [0] => H
    [1] => ll
    [2] => W
    [3] => rld */

You can use the optional third argument, $characters, that allows you to add characters that will be accepted as part of a word, such as foreign accent marks, numbers, etc.

 $str = 'H3llö Wörld';
 echo str_word_count($str, 0, '3ö'); //2
 $a = str_word_count($str, 1, '3ö');
 /* [0] => H3llö
    [1] => Wörld */

Manipulating Strings:

  1. Changing string case
  2. Comparing Strings
  3. Comparing Strings with Natural Order Algorithm
  4. Converting Strings into HTML
  5. Converting HTML Entities and Special Characters
  6. Formatting Strings
  7. Padding Strings
  8. Replacing Newlines with BR Tag and Trimming Whitespaces
  9. Converting character to code and code to character
  10. Repeat and Shuffle a String
  11. Count Characters and Words
  12. Manipulating substrings