PHP Programs

TheFestFever.com

Our aim is to connect students from campuses all over Delhi by making them aware of all Technical, Cultural, Management fests, workshops, Seminars organized by every college in Delhi.

Join Us Now

Program 47: Write a Program to find the duplicate words in a string.

duplicate_words.php

<!DOCTYPE html>  
<html>  
<head>	
	<title>PHP Programs TutorialDuniya.com</title>
</head>
<body>
	<?php  
		$str = " TutorialsDuniya.com is a witty content portal <br >that is optimized for college students <br>which mainly focuses on Computer Science <br> Subjects. Our aim to provide easily <br> accessible and well organised quality content <br> 		to all the students.";  
		$count;  

		echo "Original string is  :- <br> <br> ". $str;
		$str = strtolower($str);  
		$words = explode(" ", $str);  

		echo "Duplicate words in a given string are :- <br>";  
		for($i = 0; $i < count($words); $i++) 
		{  
			$count = 1;  
			for($j = $i+1; $j < count($words); $j++) 
			{  
				if($words[$i] == $words[$j]) 
				{  
					$count++;
					$words[$j] = "0";  
				}  
			}  
		
			if($count > 1 && $words[$i] != "0")
			{  
				echo $words[$i];  
				echo "<br>";  
			}  
		}   
	?>  
</body>  
</html>


If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.