How To Count Banned Words On Page?

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"';


	
?>
echo substr_count($article,"Evil");
if (strpos($article, "banned_words_1") == true) {
if (strpos($article, "banned_words_2") == true) {
if (preg_match('/banned_words_1/',$article))
if (preg_match('/banned_words_2/',$article))

You see any issues in these lines of code?

No issue apart from the fact that I should use either one. The str_pos or preg_match.
What is your point ?

Are you wanting to match a literal string or a variable that has a string value?

Also, if a “banned word” is the first word (position 0) will it be true or false?

1 Like

You mean it should’ve been like this:

SET 1

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\"";

Or better, it should have been like this (thus ridding the escapes).
(Less quote marks. No dbl quote marks on variables):

SET 2


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";

Or, even like this (only dbl quote marks on vari
ables. But no quoting strings):

SET 3


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";

Or better, no quoting strings or variables. No quote marks atall. Sngl or dbl:

SET 4


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;

I just hate using all those escapes. I see now what droopsnoot was hinting at.
Originally, I knew I should’ve done it like this as my subconscious was telling me not to do it like I originally did but I was not listening to myself. Or, the code was something in similar line regarding the single quotes (from where I copied it from, Stackoverflow or somewhere. I jus changed it here and there).

Let me know if I still got it wrong somewhere.
Actually people, do give ranking on the SETS how you’d do things. That way, I will know which SET you give more priority to over the others.

I usually use SET 3. Now thinking of using SET 3. But why do I get the feeling you guys would suggest SET 4 ?

Maybe you will see the problems if you change your code to

...
$article = "banned_words_1 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.";
...

This is very very strange.
When my LINE 21 goes like the following I see complete white blank page:


$article = "TEST ARTICLE

But, if I start it like this:


$article = "banned_words_1 TEST ARTICLE

Then I see only this outputted to screen:

Banned word found: “$banned_words_1”

What is your point ?

When you first introduce ‘banned_words_1’ was it a variable, or a string? Because in the preg_match() and strpos you are using it as a string.

2 Likes

'good to see error reporting is used correctly.

When blank pages occur I usually add this at the bottom of the script to confirm the script executed up to that line.
echo '<br>line: ' .__line__;

Two results:

  1. Success: - the line corresponds to the actual line of script:
  • select a possible incorrect $val and use var_dump( $val );die; to confirm $val returns the expected value.
    Rectify problem or try another $val.
  1. Failed: - the line is not rendered
    move the line halfway up the script and try again.
    On result, either move the line up or down and reiterate until the problem is pinpointed.
1 Like

Why show the above function, never use it and then check for a true strpos(…) result?

Check the PHP Online manual because the strpos(…) function never returns true.

1 Like

It’s commented out anyway, so it’s a good thing it wasn’t used.

I suspect the point of it, were it to be uncommented and used, is to do what strpos does not - i.e. return true rather than return a position or false.

1 Like

Have a read through the responses to some of your previous threads. I recall a series of explanations of the difference between quoted strings and variables. It might be in Curl experiments.

3 Likes

Used it as a variable.
$banned_words_1 = ‘Evil’;

So, you are saying, even though it has got the $banned_words_1 = it is still being overridden as a string due to the single quote ?
Q1a. Yes, or no ?
Q1b. Would these 2 be ok then ?

**$banned_words_1 = Evil;
**$banned_words_1 = “Evil”.

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”’;

Q2. In order to use them as variables, are these 2 ok ?

[code]
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”’;

[code]

Or, should it be like this:

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"';

Note the 1st & 3rd lines.

Variable.
Also, the $banned_words_count = 0;; is part of a matched words count. I did not finish the remaining part of the code that does the counting because I forgot how you do the incrementing in php. But, don’t worry, I have created a few samples (after editing codes from Stack Overflow) and some of them contained the incrementing code and so I’ll just edit my original post’s code and include the imcrement.

I copied that code formula from Stack OverFlow and so if the !== false; part seems wrong to you then that is due to the poster in Stack OverFlow getting it wrong. I just copied his formula onto my file as a comment so it can jog my memory how to code the str_pos without going to the php tutorials.

Check what I replied to WebMachine. I think I have fixed the problem you spotted.

See post #3.

I know the 1st parameter in the preg_match has to be within quotes and so I ask, I see others using single quotes and so do they have to be inside sngl quote liken this:

if (preg_match(‘/banned_words_1/’,$article))

Or, can it be within dbl, like this?

if (preg_match(“/banned_words_1/”,$article))

Your answer will solve me this issue.

How come no-one aswred to post #6 ? Answering it would solve 50% of these issues atleast!