Jump to content

Recommended Posts

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. :)

#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.

  • Like 1

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 by phpsane

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.

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!

@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: 0
Script 3a - Match: 1
Script 3a - Match: 2
Script 3a - Match: 3
Script 3a - No Match: 4
Script 3a - No Match: 5
Script 3a - No Match: 6
Script 3a - No Match: 7
Script 3a - No Match: 8
Script 3a - No Match: 9
Script 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: 0
Script 3b - Match: 1
Script 3b - Match: 2
Script 3b - Match: 3
Script 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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.