Banned Words Filter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Banned Words Filter

Post by UniqueIdeaMan »

Folks,

Q1. Do you consider the following code to be safe & sound to substitute banned words found on the page ?
If not, then maybe I try str_replace instead of preg_match ?

Code: Select all

<?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);

// 1). Set banned words.
$banned_words = array("blow", "nut", "bull****");
// 2). $curl is going to be data type curl resource.
$curl = curl_init();
// 3). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'https://www.buzzfeed.com/mjs538/the-68-words-you-cant-say-on-tv?utm_term=.xlN0R1Go89#.pbdl8dYm3X');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
// 4). Run cURL (execute http request).
$result = curl_exec($curl);
if (curl_errno($curl)) {
    echo 'Error:' . curl_error($curl);
}
$response = curl_getinfo( $curl );
if($response['http_code'] == '200' )
{
    $regex = '/\b';     
    $regex .= implode('\b|\b', $banned_words);   
    $regex .= '\b/i'; 
    $substitute = '****';
    $cleanresult = preg_replace($regex, $substitute, $result);
    echo $cleanresult;
}
curl_close($curl);
?>
Q2. How would you code it yourself ? I mean, may I see a code sample ? I've done my best.

Q3. If I change this:

Code: Select all

// 3). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'https://www.buzzfeed.com/mjs538/the-68-words-you-cant-say-on-tv?utm_term=.xlN0R1Go89#.pbdl8dYm3X');
to this:

Code: Select all

// 3). Set cURL options.
$url = "https://www.buzzfeed.com/mjs538/the-68-words-you-cant-say-on-tv?utm_term=.xlN0R1Go89#.pbdl8dYm3X";
curl_setopt($curl, CURLOPT_URL, "$url");
then it works fine. But, is it safe to do it like this ?

Q4. Where on Mini Proxy, on which line, would I add the banned words filter code (the code which you see in my Q1) or your banned words filter code so that, when banned words are found on the proxied pages, then the banned words are substituted ?

Folks, you're welcome to add a few lines of code onto the Mini Proxy so that the proxy users are blocked from viewing pages that contain banned words.
Mini Proxy is here:
https://github.com/joshdick/miniProxy/b ... iProxy.php

Finally, kindly attach the script here so that I can get hold of your update and install it on my site. You're welcome to use the proxy regular and you'e welcome to invite others to use it to.
I've googled but no luck in finding a shared webhost who will allow me to run a web proxy. :banghead:
Therefore, you're welcome to recommend many. :wink:
I'll only hire the dedicated server once I've picked-up a lot of regular users. :D
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Banned Words Filter

Post by Celauran »

Enough with the duplicate threads.

viewtopic.php?f=1&t=144493
Locked