main.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<!DOCTYPE html> <html> <body> <?php $string = "Big black bug bit a big black dog on his big black nose"; $count; //Converts the string into lowercase $string = strtolower($string); //Split the string into words using built-in function $words = explode(" ", $string); print("Duplicate words in a given string : <br>"); for($i = 0; $i < count($words); $i++) { $count = 1; for($j = $i+1; $j < count($words); $j++) { if($words[$i] == $words[$j]) { $count++; //Set words[j] to 0 to avoid printing visited word $words[$j] = "0"; } } //Displays the duplicate word if count is greater than 1 if($count > 1 && $words[$i] != "0"){ print($words[$i]); print("<br>"); } } ?> </body> </html> |