Home >>PHP Programs >How to remove duplicate characters from string

How to remove duplicate characters from string

How to remove duplicate characters from string

Enter Your Word in a text box. Remove all duplicate characters from word if any one found.
<?php
extract($_POST);
error_reporting(1);

function remove_duplicate_char($str) 
{
//add one space between all characters.
$formatted = implode(' ', str_split($str));

//conver string into array's elements 
$a = explode(" ",$formatted); 


$j = strtolower($a[0]); 
$k="";


	for ($i=1;$i<=count($a);$i++) 
	{ 
		if(strtolower($j)!=strtolower($a[$i]))
		{
		$k .= $j." ";
		} 
		$j = $a[$i]; 
	}

	return $k; 
}
if(isset($remove))
{
echo "<p align='center'>".remove_duplicate_char($n)."</p>";
}
?>
<html>
	<head>
		<title>Remove Duplicate Character</title>
	<head>
	<body>
		<form method="post">
			<table align="center" border="1">
				<Tr>
				<Td>Enter Your Word</td>
				<td><input type="text" placeholder="eg- hello" name="n"/></td>
				</tr>
				<tr>
					<td colspan="2" align="center">
					<input type="submit" value="Remove Duplicate" name="remove"/>
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

No Sidebar ads