Last active
August 6, 2017 18:43
-
-
Save ravexina/f9c585a112e4c75e7b47f69a941b4bad to your computer and use it in GitHub Desktop.
PHP - Output only lines with duplicate words (singular or plural)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Ravexina | |
// A more canonical answer has been sent here: "https://stackoverflow.com/questions/45528160/output-only-lines-with-duplicate-words" | |
// The included class.php is from here: "https://gist.github.com/tbrianjones/ba0460cc1d55f357e00b" | |
// This code tries to address the OP's comments | |
$text = "This is a best website of all the websites out there | |
This is a great website | |
Here is a website I found while looking for websites | |
Website is a cool new word | |
Playing blackjack on the blackjack table. | |
Here's a blackjack game. | |
My favorite blackjack and blackjack. | |
Something else with else and else duplicates. | |
Wow a great blackjack game."; | |
// helps us pluralize all words, so we can check the duplicates | |
include('class.php'); | |
// search for a specific term | |
$search_for = 'blackjack'; | |
// loop into each line one by one | |
foreach(explode("\n", $text) as $line) | |
{ | |
// remove special characters | |
$tline = preg_replace('/[^A-Za-z0-9\-\s]/', '', $line); | |
// create a list of words from current line | |
$words_list = preg_split('/\s+/', strtolower(trim($tline))); | |
// skip this loop if the word does not exist in this line | |
if(! in_array($search_for, $words_list)) | |
continue; | |
// convert all singular words to plural | |
foreach($words_list as $word) | |
{ | |
$w[] = Inflect::pluralize($word); | |
} | |
// if the count of words in this line was bigger that of unique | |
// words then we got some duplicates, echo this line out | |
if( count($w) > count(array_unique($w)) ) | |
echo $line . '</br>'; | |
// empty the array for next line | |
$w = []; | |
} | |
// End of the file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment