Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Duplicate Characters from string

Status
Not open for further replies.

blasterstudios

Technical User
Dec 30, 2004
128
US
I need help creating a function that will remove duplicate characters from a string of text. Basically i want to take a string submitted by the user (John Smith) and take out duplicates of the letters. So end result would be John Smit.

A better example would be Dog Frog Pog
End result will be Dog Fr P

I imagine i would have to create an array and cycle through the string pulling out each letter as it was found as a duplicate.

Anyone know how to do this?
 
fascinating. there are several ways to achieve what you want but more importantly why do you want to do it?
 
well, if you must know i'm trying to figure out a creative and unique way of associating a user group with a folder. I don't know how people normally go about doing this, but when a user registers, it creates a folder on our server and copies a few files there. I need this folder name to be unique and the folder can have multiple users so i don't want to use the username. it's actually in relation to a school, so what i'm doing is taking all the spaces out of the school's name, making sure there aren't any special characters, turning it all to lower case, and hopefully removing duplicate letters out of the name, then mixing those letters up with some numbers and creating a random 8 character string for that school.
 
ok - so this is not going to be an horrendous every second of every day task and thus performance isn't so much an issue

i think you'd be better of with uniqid() or something, but here's some code that i think does what you are looking for

Code:
<?
$input = "Now is the Time for all good quick brown foxes to come to the";
$parsed = return_parsed_input ($input, 15);
echo $parsed;
function return_parsed_input($input, $num)
{
	#remove the special characters including spaces
	$specialchars = array ("\"", "'", ";", ":" ,"\\", " "); //any others
	$input = str_replace($specialchars, "", $input);
	
	#transform to lower case
	$input = strtolower($input);
    $len = strlen($input);
	$okarray = array();
	for ($i=0; $i<$len;$i++):
		$letter = substr($input,$i,1);
		if (!in_array( $letter,$okarray)):
			$okarray[] = $letter;
		endif;		
	endfor;
   
   //remainder code adapted from php.net submission
   $max = count($okarray)>$num ? $num : count($okarray);
   
   $rand[0] = mt_rand(0, $max);
   
   for($i = 1; $i<$max; $i++){
       do{
           $check = true;
           $rand[$i] = mt_rand(0, $max);
           for($ii = 0; $ii < (count($rand)-1); $ii++){
               if($rand[$i] == $rand[$ii] && $check){
                   $check = false;
                   break;
               }
           }
       }while (!$check);
   }
    $jumble = "";
	foreach ($rand as $val):
		$jumble .= $okarray[$val];
	endforeach;
	return $jumble;
}



?>
 
it's probably self evident but the second variable in the return_parsed_input function dictates the maximum length of the resultant string. if the input string is less then the length of the input string becomes the maximum.

on re-reading your post i have not included any numerics. should not be too difficult for you to adapt, though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top