PHP Program to Count the Occurrence of Each Characters
Last Updated :
08 Jul, 2024
Given a String, the task is to count the occurrences of each character using PHP. This article explores various approaches to achieve this task using PHP, These are:
Approach 1: Using Arrays and str_split() Function
This approach involves splitting the input string into an array of individual characters using str_split() function. The array_count_values() function is then applied to obtain the count of each unique character in the array. Finally, a loop iterates through the counts and prints the occurrences for each character.
PHP
<?php
function countChars($str) {
$chars = str_split($str);
$count = array_count_values($chars);
foreach ($count as $char => $occurrences) {
echo "'$char' occurs $occurrences times.\n";
}
}
// Driver code
$str = "Welcome GeeksforGeeks";
countChars($str);
?>
Output'W' occurs 1 times.
'e' occurs 6 times.
'l' occurs 1 times.
'c' occurs 1 times.
'o' occurs 2 times.
'm' occurs 1 times.
' ' occurs 1 times.
'G' occurs 2 times.
'k' occurs 2 times.
's' occurs 2 times.
...
Approach 2: Using str_split() and array_count_values() Functions
This approach combines str_split() and array_count_values() function directly within the function. It streamlines the process by avoiding the intermediate variable for the characters array.
PHP
<?php
function countChars($str) {
$count = array_count_values(str_split($str));
foreach ($count as $char => $occurrences) {
echo "'$char' occurs $occurrences times.\n";
}
}
// Driver code
$str = "Welcome GeeksforGeeks";
countChars($str);
?>
Output'W' occurs 1 times.
'e' occurs 6 times.
'l' occurs 1 times.
'c' occurs 1 times.
'o' occurs 2 times.
'm' occurs 1 times.
' ' occurs 1 times.
'G' occurs 2 times.
'k' occurs 2 times.
's' occurs 2 times.
...
Approach 3: Using preg_match_all() Function
This approach leverages regular expressions through preg_match_all() to match all characters in the Unicode string (/./u). The resulting matches are then processed using array_count_values() to get the count of each character, and a loop prints the occurrences.
PHP
<?php
function countChars($str) {
preg_match_all('/./u', $str, $matches);
$count = array_count_values($matches[0]);
foreach ($count as $char => $occurrences) {
echo "'$char' occurs $occurrences times.\n";
}
}
// Driver code
$str = "Welcome GeeksforGeeks";
countChars($str);
?>
Output'W' occurs 1 times.
'e' occurs 6 times.
'l' occurs 1 times.
'c' occurs 1 times.
'o' occurs 2 times.
'm' occurs 1 times.
' ' occurs 1 times.
'G' occurs 2 times.
'k' occurs 2 times.
's' occurs 2 times.
...
Approach 4: Using count_chars() Function
Another efficient way to count the occurrences of each character in a string is by using the count_chars() function. This function returns an array with the frequency of every byte-value (character) used in the string.
Example: This approach is particularly useful when you need a quick and efficient way to count the occurrences of each character in a string using PHP.
PHP
<?php
function countCharacterOccurrences($inputString) {
// Use count_chars() to get the frequency of each character
$charCounts = count_chars($inputString, 1);
// Iterate through the resulting array and print the occurrences of each character
foreach ($charCounts as $char => $count) {
echo chr($char) . " => " . $count . "\n";
}
}
// Sample usage
$inputString = "GeeksforGeeks";
countCharacterOccurrences($inputString);
?>
OutputG => 2
e => 4
f => 1
k => 2
o => 1
r => 1
s => 2
Similar Reads
How to read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_
4 min read
How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in
2 min read
How to remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re
3 min read
How to remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re
3 min read
How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t
2 min read
PHP Program to Find the Number Occurring Odd Number of Times Write a PHP program for a given array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space. Examples : Input: arr = {1, 2, 3, 2, 3, 1, 3}Output : 3 Input: arr = {5, 7, 2, 7, 5, 2, 5}Ou
3 min read