Open In App
Related Articles

PHP | Different characters in the given string

Improve Article
Improve
Save Article
Save
Like Article
Like

We have string str and we need to find the string with all unique characters from the given string. All different characters should be printed in ascending order.

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

The problem can be solved using PHP in built function for collecting all the unique characters used in the given string.The in built function used for the given problem is:

  1. count_chars():The in built function has parameter which contains return mode an integer type which are as such 0, 1, 2, 3, 4 in which return mode 3 returns the string of all different characters used in the given string in ascending order.
  2. Note: The string is case-sensitive.

Example 1:




<?php
// PHP code to find string containing unique 
// characters in the given string
  
$str = "Geeksforgeeks";
$result = (count_chars($str, 3));
  
echo ($result);
?>


Output:

Gefgkors

Example 2:




<?php
// PHP code to find string containing unique 
// characters  in the given string
  
$str = "GeeksforGeeks";
$result = (count_chars($str, 3));
  
echo ($result);
?>


Output:

Gefkors

Example3:




<?php
// PHP code to find string containing unique 
// characters in the given string
  
$str = "Computer Science Portal";
$result = (count_chars($str, 3));
  
echo ($result);
?>


Output:

CPSaceilmnoprtu

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 16 Mar, 2018
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials