PHP Program to Print All Duplicate Characters in a String Last Updated : 08 Jul, 2024 Comments Improve Suggest changes Like Article Like Report This article will show you how to print duplicate characters in a string using PHP. Detecting and printing duplicate characters in a string is a common task in programming. In this article, we will explore various approaches to achieve this.Table of ContentUsing array_count_values() FunctionUsing Associative ArrayUsing a Regular Expression and preg_match_all()Using array_count_values() FunctionThe array_count_values() function is used to count the occurrences of each value in an array, simplifying the process. PHP <?php function printDuplicates($str) { $charCount = array_count_values(str_split($str)); // Print duplicate characters echo "Duplicate characters: "; foreach ($charCount as $char => $count) { if ($count > 1) { echo "$char "; } } } // Driver code $str = "Welcome to GeeksforGeeks"; printDuplicates($str); ?> OutputDuplicate characters: e o G k s Using Associative ArrayOne of the most basic methods is to use an associative array to keep track of the occurrence of each character in the string. PHP <?php function printDuplicates($str) { $charCount = []; // Iterate through each character // in the string for ($i = 0; $i < strlen($str); $i++) { $char = $str[$i]; // Check if the character is already // in the associative array if (isset($charCount[$char])) { $charCount[$char]++; } else { $charCount[$char] = 1; } } // Print duplicate characters echo "Duplicate characters: "; foreach ($charCount as $char => $count) { if ($count > 1) { echo "$char "; } } } // Driver code $str = "Welcome to GeeksforGeeks"; printDuplicates($str); ?> OutputDuplicate characters: e o G k s Using a Regular Expression and preg_match_all()Another approach to print duplicate characters in a string is by using a regular expression in combination with the preg_match_all() function. This method is particularly useful for efficiently identifying and handling characters within the string.Example: This approach offers a better way to detect and print duplicate characters in a string, especially when dealing with Unicode characters or requiring efficient pattern matching. PHP <?php function printDuplicateCharacters($inputString) { // Initialize an empty array to store character counts $charCounts = []; // Use preg_match_all to get all characters preg_match_all('/./u', $inputString, $matches); // Iterate through each character and count occurrences foreach ($matches[0] as $char) { if (isset($charCounts[$char])) { $charCounts[$char]++; } else { $charCounts[$char] = 1; } } // Print characters that appear more than once foreach ($charCounts as $char => $count) { if ($count > 1) { echo "Character '$char' appears $count times.\n"; } } } // Sample usage $inputString = "programming"; printDuplicateCharacters($inputString); ?> OutputCharacter 'r' appears 2 times. Character 'g' appears 2 times. Character 'm' appears 2 times. Comment More infoAdvertise with us Next Article PHP Program to Print All Duplicate Characters in a String V vkash8574 Follow Improve Article Tags : PHP PHP-string Geeks Premier League 2023 Similar Reads How to remove duplicate characters from a String in Scala? In this article, we will learn how to remove duplicate characters from a string in Scala using different approaches. First, we will look through the Brute-Force Approach and then use different standard libraries. Examples: Input: String = "hello"Output: helo Input: String = "Geeks"Output: Geks Naive 4 min read 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 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 to check substring in a string In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lo 2 min read PHP str_pad to print string patterns str_pad: Pad a string to a certain length with another string. Syntax:- str_pad (input, pad_length, pad_string_value, pad_type) It returns the padded string. Parameters Description input:-The input string. pad_length:-If the value of pad_length is negative, less than, or equal to the length of the i 1 min read PHP Different characters in the given string Different characters in a given string refer to the unique, non-repeating characters that appear within the string. Identifying these characters involves recognizing each distinct character while excluding any duplicates. This concept is useful in various programming and data analysis tasks where un 2 min read How to check if a String contains a Specific Character in PHP ? In PHP, determining whether a string contains a specific character is a common task. Whether you're validating user input, parsing data, or performing text processing, PHP provides several methods to check for the presence of a particular character within a string efficiently. Here are some common a 2 min read How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri 1 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 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 Like