Php Lovers,
I tried building a script that counts how many banned words exist on the page and echos the count result.
On 1st attempt, not making use of arrays and so let’s do it without it first.
Now, can anyone figure why the following attempt gives a blank white page.
Tried in 2 ways. 1st: str_pos; 2nd: preg_match.
<?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"';
?>