Skip to content

Instantly share code, notes, and snippets.

@Fed0t
Created November 21, 2018 14:17
Show Gist options
  • Save Fed0t/b6bf71f2dcaf90631e08f7ff4e624e07 to your computer and use it in GitHub Desktop.
Save Fed0t/b6bf71f2dcaf90631e08f7ff4e624e07 to your computer and use it in GitHub Desktop.
Simple words scanner class in PHP
<?php
$needleWords = 'work,hard,learn';
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris ,work, nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in hard, culpa qui officia deserunt mollit anim id est laborum.';
$scanner = new WordsScanner($needleWords,$text);
$scanner->scanText();
class WordsScanner {
private $words;
private $text;
public function __construct($words,$text){
$this->words = $words;
$this->text = $text;
}
public function wordsToArray(){
$words = explode(',',$this->words);
return $words;
}
public function reCheck($word){
$bannedWords = $this->wordsToArray();
return in_array($word,$bannedWords);
}
public function wordFound($word){
//You can alert someone
echo sprintf('Word found: %s',$word).PHP_EOL;
}
public function scanText(){
$bannedWords = $this->wordsToArray();
foreach($bannedWords as $word){
if(strpos($this->text, $word) ){
if($this->reCheck($word)){
$this->wordFound($word);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment