phpsane Posted August 28, 2017 Share Posted August 28, 2017 Php Experts, Trying to build a script that counts how many banned words exist on the current page and then echos the count result and the banned words existing on the page. On 1st attempt, not making use of arrays and so let's do it without it first. I tried doing it in 3 different ways but need your help to finish them. SAMPLE 1 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); /* Formula: returns true if $needle is a substring of $haystack function contains($needle, $haystack) { return strpos($haystack, $needle) !== false; } */ $banned_words_count = 0; $article = "TEST ARTICLE This is an article to see if our php script can spot any banned words in it or not. These are the banned words: Evil, Devil. Since this article contains these 2 banned words then our php script should be able to spot them and count them to 2. End of article."; $banned_words_1 = 'Evil'; $banned_words_2 = 'Devil'; if (strpos($article, "banned_words_1") == true) { echo 'Banned word found: "$banned_words_1"'; } if (strpos($article, "banned_words_2") == true) { echo 'Banned word found: "$banned_words_2"'; } if (preg_match('/banned_words_1/',$article)) echo 'Banned word found: "$banned_words_1"'; if (preg_match('/banned_words_2/',$article)) echo 'Banned word found: "$banned_words_2"'; ?> SAMPLE 2 /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); //script 1: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array //http://php.net/manual/en/function.in-array.php //Simplest check. $content = 'evil'; $targets = array("evil", "devil"); if (in_array($content, $targets)) { echo "Script 1 - Found Banned Word: HOW TO DISPLAY MATCHING WORD HERE ?"; } ?> SAMPLE 3 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); //script 6: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string //$email = $_POST['email']; $email = "[email protected]"; if(preg_match("/\b(evil|devil)\b/", $email)){ echo "Script 6 - Found Banned Word: HOW DO I DISPLAY THE FOUND BANNED WORD HERE ? WOULD LIKE TO SEE SNIPPET EXAMPLE"; } ?> I know you'd say the 1st example is not good. But let's assume we did do it that way. How would you solve the issue on it ? Infact, how would you solve the issues on these 3 samples ? Any example codes welcome. Quote Link to comment https://forums.phpfreaks.com/topic/304737-how-to-display-matching-banned-words-on-screen/ Share on other sites More sharing options...
requinix Posted August 28, 2017 Share Posted August 28, 2017 #1 won't work because you somehow expect PHP to know that "banned_words_1" in a string is supposed to be mean a variable of the same name. It doesn't. #2 won't work because you're checking the entirety of the content against the list of banned words. It's functional but won't do what you want. #3 could actually work but it relies on building a gigantic list of words and sticking it into a regex which is a bad idea. There are a couple options. Performance-wise, I think the best would be 1. Use an strtok() loop to read each word (tokenized by spaces) from the text. 2. Normalize the word by stripping punctuation, like periods and commas, and lowercasing. 3. Put each word into an array as a key. 4. After tokenizing the whole text, loop through your bad words list (which is already normalized) and check each if it is in the array. 1 Quote Link to comment https://forums.phpfreaks.com/topic/304737-how-to-display-matching-banned-words-on-screen/#findComment-1550275 Share on other sites More sharing options...
phpsane Posted August 28, 2017 Author Share Posted August 28, 2017 @requinix, Thanks Lazy Administrator! Mmm. Would appreciate a small sample code (if & when you have the time). I'm sure other newbies would appreciate it. Not aware of the strtok(). Gonna research on it now. Quote Link to comment https://forums.phpfreaks.com/topic/304737-how-to-display-matching-banned-words-on-screen/#findComment-1550284 Share on other sites More sharing options...
phpsane Posted August 28, 2017 Author Share Posted August 28, 2017 (edited) Php Lovers, Below are a few code samples. 9. They check for banned words on a page/content. And then supposed to alert which banned words have been spotted. Anyway, I placed them all on a single page and then view the page in Chrome. Instead of showing 9 results or outputs I only see 1. Why is that ? Why are not the other codes getting triggered ? I only see this as the result or output: Script 2 - Found Banned Word: evil That means, only the 2nd script is managing to output the results while the other 8 are not. What do you make out of all this ? Anyway, here are the 9 scripts: Script 1 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); /* Formula: returns true if $needle is a substring of $haystack function contains($needle, $haystack) { return strpos($haystack, $needle) !== false; } */ //script 1: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array //http://php.net/manual/en/function.in-array.php //Simplest check. $content = 'Evil & devillish they were!'; $targets = array("evil", "devil"); if (in_array($content, $targets)) { print_r([$targets]); echo '<br>' .date('H:i:s'); } ?> <br> Script 2 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //script 2: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string $banned = array("evil", "devil"); //$email = $_POST['email']; $email = "[email protected]"; foreach ($banned as $ban) { if (stripos($email, $ban) !== FALSE) { echo "Script 2 - Found Banned Word: $ban"; return true; } } echo "No banned words found."; return false; ?> <br> Script 3 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //script 3: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string $banned = array("evil", "devil"); //$email = $_POST['email']; $email = "[email protected]"; foreach ($banned as $ban) { if (stripos($email,$ban) > -1){ echo "Script 3 - Found Banned Word: $ban"; }else{ echo 'Email signed up!'; } } ?> <br> Script 4 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //script 4: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array $content = "There are evil devils all over the place!"; $banned_words = array('evil', 'devil'); foreach($banned_words as $banned) { $place = strpos($content, $banned); if (!empty($place)) { echo "Script 4 - Found Banned Word: $band"; exit; } else { echo "No banned words found!"; } } ?> <br> Script 5 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $banned_words = array("trash", "rubbish"); $content = "This is my rubbish text"; $banned = false; for($i=0; $i < count($banned_words); $i++) { if(strrpos($content, $banned_words[$i]) != FALSE ) { $banned = true; echo "Script 5 - Found Banned Word:"; print_r($banned_words[$i]); break; } } ?> <br> Script 6 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //script 6: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string //$email = $_POST['email']; $email = "[email protected]"; if(preg_match("/\b(evil|devil)\b/", $email)){ echo "Script 6 - Found Banned Word: HOW DO I DISPLAY THE FOUND BANNED WORD HERE ? WOULD LIKE TO SEE SNIPPET EXAMPLE"; } ?> Script 7 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //script 7: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array //Checking arrays against other arrays $keywords = array('evil','devil','satan'); $targets = array('monster','satan','devil'); foreach ( $targets as $string ) { foreach ( $keywords as $keyword ) { if ( strpos( $string, $keyword ) !== FALSE ) { echo "Script 7 - Found Banned Word: $string"; } } } ?> <br> Script 8 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //script 8: //$email = $_POST['email']; $email = "[email protected]"; if(preg_match("/\b(evil|devil)\b/", $email)){ echo "Script 6 - Found Banned Word: HOW DO I DISPLAY THE FOUND BANNED WORD HERE ? WOULD LIKE TO SEE SNIPPET EXAMPLE"; } ?> <br> Script 9 <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //script 9 /* Formula: returns true if $needle is a substring of $haystack function contains($needle, $haystack) { return strpos($haystack, $needle) !== false; } */ $banned_words_count = 0; $article = "TEST ARTICLE This is an article to see if our php script can spot any banned words in it or not. These are the banned words: Evil, Devil. Since this article contains these 2 banned words then our php script should be able to spot them and count them to 2. End of article."; $banned_words_1 = 'Evil'; $banned_words_2 = 'Devil'; if (strpos($article, "banned_words_1") == true) { echo 'Banned word found: "$banned_words_1"'; } if (strpos($article, "banned_words_2") == true) { echo 'Banned word found: "$banned_words_2"'; } if (preg_match("/banned_words_1/",$article)) echo 'Banned word found: "$banned_words_1"'; if (preg_match("/banned_words_2/",$article)) echo 'Banned word found: "$banned_words_2"'; echo '<br>line: ' .__line__; ?> <br> Edited August 28, 2017 by phpsane Quote Link to comment https://forums.phpfreaks.com/topic/304737-how-to-display-matching-banned-words-on-screen/#findComment-1550287 Share on other sites More sharing options...
requinix Posted August 28, 2017 Share Posted August 28, 2017 In order, See my previous post, works, false == 0, 0 is empty, same as the one two before, see my previous post, works but expensive, literally same as two before, see my god damn previous fscking post. Quote Link to comment https://forums.phpfreaks.com/topic/304737-how-to-display-matching-banned-words-on-screen/#findComment-1550288 Share on other sites More sharing options...
phpsane Posted September 2, 2017 Author Share Posted September 2, 2017 Php Experts, You have seen 9 code samples from me. Now, I would like to see atleast 1 code sample from each of you on how you would code it and what you deem it to be the best way to code it. Your code sample must: 1. Fetch a webpage; 2. Scan the webpage for banned words and show error along with all the banned words found (if any is found). We newbies, can then learn from your samples. To start-off with, how-about a sample from you requinix ? Your senior members may follow suit if you take the lead! I really need to close this thread as ANSWERED! Been waiting 5 days now. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/304737-how-to-display-matching-banned-words-on-screen/#findComment-1550608 Share on other sites More sharing options...
requinix Posted September 2, 2017 Share Posted September 2, 2017 No. You've got other threads about this same issue, look to them if you want code. Quote Link to comment https://forums.phpfreaks.com/topic/304737-how-to-display-matching-banned-words-on-screen/#findComment-1550622 Share on other sites More sharing options...
phpsane Posted September 3, 2017 Author Share Posted September 3, 2017 @Newbies, The following 2 codes are working for me. Try them. <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //script 3a: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string $banned_words = array("0","1","2","3","4","5","6","7","8","9","f***o**"); $content = "0,1,2,3,bog off"; foreach ($banned_words as $ban) { if (stripos($content,$ban) > -1){ echo "Script 3a - Match: $ban<br>"; //Place "exit;" if you want it to stop after finding a match. }else{ echo "Script 3a - No Match: $ban<br>"; } } ?> Showing output as: Script 3a - Match: 0Script 3a - Match: 1Script 3a - Match: 2Script 3a - Match: 3Script 3a - No Match: 4Script 3a - No Match: 5Script 3a - No Match: 6Script 3a - No Match: 7Script 3a - No Match: 8Script 3a - No Match: 9Script 3a - No Match: f***o** <?php //script 3b: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string $banned_words = array("0","1","2","3","f***o**"); $content = "0,1,2,3,4,5,6,7,8,9,bog off"; foreach ($banned_words as $ban) { if (stripos($content,$ban) > -1){ echo "Script 3b - Match: $ban<br>"; //Place "exit;" if you want it to stop after finding a match. }else{ echo "Script 3b - No Match: $ban<br>"; } } ?> Showing output as: Script 3b - Match: 0Script 3b - Match: 1Script 3b - Match: 2Script 3b - Match: 3Script 3b - No Match: f***o** NOTE: Notice the difference between the 2 script's outputs and their variables ($content & $banned_words). @Admin/Mod, If you deem the codes are good enough then you may close this thread. Else, provide a sample yourself and I will pick that as BEST ANSWER and close the thread. Thank You! Quote Link to comment https://forums.phpfreaks.com/topic/304737-how-to-display-matching-banned-words-on-screen/#findComment-1550662 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.