How To Count Banned Words On Page?

From where did you glean this piece of “information”?

Single or double quotes makes no difference, the result is still that the first parameter (Pattern to search for) passes to the function is a string. That string being: banned_words_1
Can you see how that may be a problem?

Clue: When you define:-

$banned_words_1 = 'Evil';

Where are you using that variable?

1 Like

I see. So, whenever I define a variable then it does not matter whether I use a quote or not or a sngl or a double one.
What matters is, when I am calling it, I should use the dbl quotes or no quotes but not the sngle quotes. Else, it gets counted as a string.
Q1. Right ?

Q2. From your hint, I now deem this to be wrong:


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

And this to be correct. Correct ?

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

Q3. Is not the following correct ?
(Note the no quotes, sngl quotes and dbl quotes during defining the variables and calling them).


$variable_1 = hate;
$variable_2 = "love";
$variable_3 = 'like';

I do not $variable_1 SamA74's reponses;
I "$variable_3" him;
Because I $variable_2 the way he helps me out!;

EDIT 1 - I am unable to test this code as I still see blank page as I have other codes on the same file.
But noticed I get error if I double quote the variable name such as:
I “$variable_3” him;

EDIT 2 - I removed the other codes and I see this error:

Notice: Use of undefined constant hate - assumed ‘hate’ in C:\xampp\htdocs\e_id\new 1.php on line 13
I do not hate SamA74’s reponses; I like him; Because I love the way he helps me out!

It has jogged my memory that in order to define a variable, I must quote the value. Else, it gets counted as a constant.

All this means is this code is working fine:


$variable_1 = 'hate';
$variable_2 = "love";
$variable_3 = 'like';

echo "I do not $variable_1 SamA74's reponses;
I $variable_3 him;
Because I $variable_2 the way he helps me out!";

And so, what has jogged my memory from this experiment ?

  1. The variable value must be quoted. Sngle or dbl quoted.
  2. When calling the variable to display it’s value, never quote the variable name with dbl quotes.
    This is ok:
    I $variable_3 him;

And this too is ok if you want the variable value to be quoted:
I ‘$variable_3’ him;
(Before this experiment, I did not think this was ok as I thought the variable name would be turned into a string and the variable name would be quoted literally).

But not this:
I “$variable_3” him;
(Before this experiment, I thought this was ok).

Guys,

What is the "" b for on this preg_match ?


<?php
//script 6: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string
//$email = $_POST['email'];
$email = "evil@devil.com";

if(preg_match("/\b(evil|devil)\b/", $email)){
    echo "Found";
}

?>

Fellow php Programmers,

I have found many code samples (mentioned below) and adjusted according to my needs. Just one trouble. On each of the following ways I coded, how do I write code to echo the banned word who’s match has been found on the page ?
Notice the CAPITAL SENTENCE on all samples.

<?php

/*
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
//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.
$yourWord='evil';
$targets = array("evil", "devil");

if (in_array($yourWord, $targets)) {
    echo "Sciptt 1 - Found Banned Word: $targets";
}
?>
<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 2: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array
$keywords=array('one','two','three');
$targets=array('eleven','six','two');
foreach ( $targets as $string ) 
{
  foreach ( $keywords as $keyword ) 
  {
    if ( strpos( $string, $keyword ) !== FALSE )
     { echo "Script 2 - Found Banned Word: $targets"; }
  }
}
?>
<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

<?php
//script 3: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array
$string = "There are evil devils all over the place!";
$banned_words = array('evil', 'devil');
foreach($banned_words as $band_word) {
    $place = strpos($string, $band_word);
    if (!empty($place)) {
        echo "Script 3 - Found Banned Word: $band_word";
        exit;
    } else {
        echo "No banned words found!";
    }
}
?>
<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 4 : https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string
foreach ($banned as $v) {
    if (stripos($email,$v) > -1){
        echo "Script 4 - Found Banned Word: $v";
    }else{
        echo 'Email signed up!';
    }
}

?>
<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 5: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string
$banned = array("evil", "devil", "blah", "*", "#", "$", "%");
//$email = $_POST['email'];
$email = "evil@devil.com";
$banned = false;

for($i=0; $i < count($banned); $i++) {
    if(strrpos($email, $banned[$i]) != FALSE ) {
       $banned = true;
       break;
    }
}
echo "Script 5 - Found Banned Word: $banned[$i]";

?>
<?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 = "evil@devil.com";

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 THE SEE SNIPPET EXAMPLE";
}

?>
<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 7: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string
$banned = array("evil", "devil", "blah", "*", "#", "$", "%");
//$email = $_POST['email'];
$email = "evil@devil.com";

    foreach ($banned as $ban) {
        if (stripos($email, $ban) !== FALSE) {
            echo "Script 7 - Found Banned Word: $ban";
            return true;
        }
    }
    echo "No banned words found.";
    return false;
?>

I’d appreciate it, if you guys add the missing code to display the matched banned word found on the webpage and paste the code so we newbies flooding this way can see the missing puzzle peaces and learn from them! I need help on SCRIPT 6. :slight_smile:

Would appreciate it if you can check the other 5 scripts out. Especially check SCRIPT 5 as I am not sure if I done it correct.

Also, on SCRIPT 1, I get this echo:
Script 1 - Found Banned Word: Array

Thanks!

http://regular-expressions.mobi/refquick.html?wlr=1

1 Like

Why don’t you give it a try yourself.
I’m sure you’ve learned enough by now to be able to echo a variable?

1 Like

Echoing a variable is one thing but echoing the matched banned word found on the page is different. Hence, stuck for 3 nights tonight.
have been googling. No luck.

Ah! So, you learnt regex from that link, did you ?
Found-out word boundary is \b.

Thanks! :slight_smile:

EDIT: Ok, I tried DarthGuido,

Why don’t you check them out and give feed-back ?
How-about you give SCRIPT 7 a try on #post 24 ? I need your feed-back on SCRIPT 5 also.

Thanks!

Wrong.

^ This.

It is quite apparent that you still have no real grasp of variables and strings, and you will continue to go round and round in circles until you have a clear understanding of these. As @droopsnoot says, the information has all been given to you in previous topics. Please go and read it and re-read it until you understand and remember it. There is no point in continuing to ask new questions before you have grasped the basics here.

5 Likes

It is essential PHP variables are used correctly and it appears numerous hints and examples have not been understood.

Here is a link to a Php Variable Tutorial

Sincerely hope you are able to understand the concepts and all the online examples are thoroughly tried and extensively tested.

3 Likes

I didn’t try it, but the echo statement looks good. Did you try it? And did it give you the correct result?

1 Like

Techno Bear,

I thought the following was an ok variable:

<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$car_1 = ferrari;

echo $car_1;

As you can see the value of the variable is not quoted. According to me (and it seems according to you too) this should work. But I get error:

Notice: Use of undefined constant ferrari - assumed ‘ferrari’ in C:\xampp\htdocs\e_id\new 1.php on line 11
ferrari

Line 11 is: $car_1 = “ferrari”;
Now tell me, why is php counting the variable as a constant ?

Now, let’s quote the value of this variable and see whether the error disappears or not.
RESULT: I see the screen echoing: ferrari.
No errors on screen.
That means, php has no longer taken the variable as a constant. And that is, all due to quoting the variable’s value.
I get the same no error result if I single quote the variable value too.

Look:

SAMPLE 1

$car_1 = ferrari;
echo $car_1;

RESULT: Error:
Notice: Use of undefined constant ferrari - assumed ‘ferrari’ in C:\xampp\htdocs\e_id\new 1.php on line 11

SAMPLE 2

$car_1 = ferrari;
echo "$car_1";

**RESULT: **
Notice: Use of undefined constant ferrari - assumed ‘ferrari’ in C:\xampp\htdocs\e_id\new 1.php on line 11
ferrari

SAMPLE 3

$car_1 = 'ferrari';
echo $car_1;

RESULT: ferrari

SAMPLE 4

$car_1 = 'ferrari';
echo "$car_1";

RESULT: ferrari

SAMPLE 5

$car_1 = "ferrari";
echo $car_1;

RESULT: ferrari

SAMPLE 6

$car_1 = "ferrari";
echo "$car_1";

RESULT: ferrari

And yes, I did put the following error reporting code on the top of each SAMPLES during testing:

<?php
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

If you have any doubts, fire-up Xamp/Wamp and see for yourself.
I have php 7.
In short, test this snippet and see the result for yourself:

<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$car_1 = ferrari;
$car_2 = 'transam';
$car_3 = "lamborghini";

echo $car_1;
echo '$car_1';
echo "$car_1";

echo $car_2;
echo '$car_2';
echo $car_2;

echo $car_3;
echo '$car_3';
echo $car_3;
    
?>

Try echoing $car_1 between single quotes.

Edit:
Ahh I noticed in your amendments you tried that option. I hope you noticed the difference, it is a very useful.

That won’t show the value of the variable but literally output:

$car_1.

I know the single quote outputs “as is” (literally) like a string.
Don’t worry, I don’t need to experiment that.
Check my post #22.
I made these statements:

And so, what has jogged my memory from this experiment ?

  1. The variable value must be quoted. Sngle or dbl quoted.
  2. When calling the variable to display it’s value, never quote the variable name with dbl quotes.
    This is ok:
    I $variable_3 him;
    And this too is ok if you want the variable value to be quoted:
    I ‘$variable_3’ him;
    (Before this experiment, I did not think this was ok as I thought the variable name would be turned into a string and the variable name would be quoted literally).

But not this:
I “$variable_3” him;
(Before this experiment, I thought this was ok).

Techno Bear claims my 1st statement is wrong. Check his post#28.
Infact, I used to believe ExACTLY like him before the test. After the test, my opinion differed from his.
What is your conclusion regarding my 1st statement BEFORE looking at my experiment mentioned on post #32 ?
And, what is your conclusion regarding my 1st statement AFTER looking at my experiment mentioned on post #32 ?
Others are welcome to comment.And so, people, do make your comments (rather than LIKE somebody else’s post) so I can read your replies. You never know, you might give some form of insight to something that the other person (the person who’s post you LIKE) has not.

Thanks

I’d appreciate it if you answer my questions on post #24.
I am stuck there for 24hrs now.

Thanks

John_Betong, droopsnoot, SamA74 & cpRadio,

Q1. How would you fix this script ?
This attempt is a mess:

//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)) {
    print_r($targets);
}
?>
<br>

It shows result like this:

Array ( [0] => evil [1] => devil )

That is not what I want. I want it to echo the found banned word.
Eg.
Script 1 - Found Banned Word: evil

So, let’s revert back to the original, half cooked code, that I managed to burn it:

//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 - $targets ?";
}
?>
<br>

I get this error:
Notice: Array to string conversion in C:\xampp\htdocs\e_id\new 1.php on line …
(It is an indication to this line:
echo “Script 1 - $targets ?”;)

I just added that line as you suggested but my script is not executing all the lines.
Why is that ?
There are 8 snippets but only 2 are getting executed! Strange!
Ok, it does not matter whether all the 8 are perfect or not. It should either show all their results or errors if any snippet is out of touch. Right ?

<?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;
}
*/

//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 ?";
}
?>
<br>


<?php
//script 2: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

$banned = array("evil", "devil");
//$email = $_POST['email'];
$email = "evil@devil.com";

    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>


<?php
//script 3: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

$banned = array("evil", "devil");
//$email = $_POST['email'];
$email = "evil@devil.com";

foreach ($banned as $ban) {
    if (stripos($email,$ban) > -1){
        echo "Script 3 - Found Banned Word: $ban";
    }else{
        echo 'Email signed up!';
    }
}

?>
<br>


<?php
//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 $band_word) {
    $place = strpos($content, $band_word);
    if (!empty($place)) {
        echo "Script 4 - Found Banned Word: $band_word";
        exit;
    } else {
        echo "No banned words found!";
    }
}
?>


<?php
//script 5: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

$banned = array("evil", "devil");
//$email = $_POST['email'];
$email = "evil@devil.com";
$banned = false;

for($i=0; $i < count($banned); $i++) {
    if(strrpos($email, $banned[$i]) != FALSE ) {
       $banned = true;
       break;
    }
}
echo "Script 5 - Found Banned Word: $banned[$i]";

?>
<br>


<?php
//script 6: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

//$email = $_POST['email'];
$email = "evil@devil.com";

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

?>


<?php
//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>


<?php

/*
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__;
    
?>

Notice your LINE at the final line, John_Betong! :wink:
Let us see if we can spot why the other snippets are not executing without the var_dump. :wink:
This is what I see:

Script 1 - Found Banned Word: HOW TO DISPLAY MATCHING WORD HERE ?
Script 2 - Found Banned Word: evil

http://php.net/manual/en/function.return.php

If return is called from within the main script file, then script execution ends.

Typically return is inside a function.

Perhaps instead of returning Boolean TRUE you could assign that to a variable and test the variable later?

2 Likes

I would have introduced more echo '<br>line: ' .__line__; to see which line is giving the problem. Far quicker than cutting, pasting and composing the question.

I can see the problem but think if you manage to find the error using this method it may be helpful in your future programming tasks.

At no point did I say that the value should not be quoted. You are still guessing at answers. Of course the value should be in quotes if it is a string.

The value of a variable need not be a string.

Your first statement is wrong, as you would have discovered without all the guessing and “experimenting” if you had taken the advice given to revise variables and strings until you understand and remember the basics.

2 Likes

Please don’t take this the wrong way @uniqueideaman , but you have referred several times to being able to write desktop exes and the like without any problems, and offered to do so in exchange for help.

Can I ask what programming language you write them in? Because I am struggling to think of a popular programming language that handles variables in a significantly different way to PHP. I am sure there are some, and I’m sure I am out of touch with some of the modern languages, I just wondered what you use that causes such a difficulty with what should be quite a basic task, certainly something to be mastered prior to fiddling around with CURL and the like.

5 Likes