PHP Different characters in the given string Last Updated : 18 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 : GefkorsExplanation, the unique characters in the givenstring $str in ascending order are G, e, f, k, o, r and sInput : $str = "Computer Science Portal"Output : CPSaceilmnoprtuExplanation, the unique characters in the given string $str are C, P, S, a, c, ei, l, m, n, o, p, r, t and uTable of ContentUsing count_chars()Using array_unique and sortUsing 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 CPSaceilmnoprtuUsing array_unique and sortUse 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 Comment More infoAdvertise with us Next Article PHP Different characters in the given string A akash1295 Follow Improve Article Tags : Technical Scripter Web Technologies PHP PHP-string PHP-function +1 More 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 How to replace multiple characters in a string in PHP ? A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP.Using the str_repla 3 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 PHP | DOMCharacterData substringData() Function The DOMCharacterData::substringData() function is an inbuilt function in PHP which is used to extracts a range of data from the node. Syntax: string DOMCharacterData::substringData( int $offset, int $count ) Parameters: This function accept two parameters as mentioned above and described below: $off 2 min read Concatenation of two strings in PHP In this article, we will concatenate two strings in PHP. There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right 2 min read How to extract Numbers From a String in PHP ? Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.Here we have some 3 min read Like