PHP Different characters in the given string
Last Updated : 18 Jul, 2024
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 understanding the diversity of characters is important.
Examples:
Input : $str = "GeeksforGeeks"
Output : Gefkors
Explanation, the unique characters in the given
string $str in ascending order are G, e, f, k,
o, r and s
Input : $str = "Computer Science Portal"
Output : CPSaceilmnoprtu
Explanation, the unique characters in the given
string $str are C, P, S, a, c, ei, l, m, n, o,
p, r, t and u
Using count_chars()
The count_chars() function in PHP can be used to collect all unique characters from a string. When used with return mode 3, it returns a string with all different characters used in the given string in ascending order.
Example : The uniqueCharacters function in PHP returns a string containing unique characters from the input string. It uses count_chars($str, 3).
php <?php function uniqueCharacters($str) { return count_chars($str, 3); } // Example 1 $str1 = "GeeksforGeeks"; echo uniqueCharacters($str1); // Output: Gefkors echo "\n"; // Example 2 $str2 = "Computer Science Portal"; echo uniqueCharacters($str2); // Output: CPSaceilmnoprtu ?>
OutputGefkors CPSaceilmnoprtu
Using array_unique and sort
Use array_unique and sort in PHP to find unique characters from a string and sort them in ascending order. Convert the string to an array, remove duplicates, sort the array, and join it back into a string.
Example: The uniqueCharacters function in PHP returns a string of unique, sorted characters from the input string. It splits the string, removes duplicates, sorts the characters, and joins them back
PHP <?php function uniqueCharacters($str) { $chars = str_split($str); $uniqueChars = array_unique($chars); sort($uniqueChars); return implode('', $uniqueChars); } echo uniqueCharacters("GeeksforGeeks"); echo "\n"; echo uniqueCharacters("Computer Science Portal"); ?>
OutputGefkors CPSaceilmnoprtu
Similar Reads
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
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 get string between two characters in PHP ? A string is a sequence of characters stored in the incremental form in PHP. A set of characters, one or many can lie between any two chosen indexes of the string. The text between two characters in PHP can be extracted using the following two methods : Table of ContentUsing substr() Method: Using fo
4 min read
How to Extract Words from given String in PHP ? Extracting words from a given string is a common task in PHP, often used in text processing and manipulation. Below are the approaches to extract words from a given string in PHP: Table of Content Using explode() functionUsing regular expressionsUsing explode() functionIn this approach, we are using
1 min read