0

I am trying to use bad words filter but I want to load the bad words from a txt file on my server but some how failed to do so.here is my bad words filter code

function badWordFilter(&$text, $replace)
{
$patterns = array(
    '/butt/i',
    '/poop/i',
    '/crap/i'
);

$replaces = array(
    'b***',
    'p***',
    'c***'
);

$count = 0;
if($replace){
    $text = preg_replace($patterns, $replaces, $text, -1, $count);
} else {
    foreach($patterns as $pattern){
        $count = preg_match($pattern, $text);
        if($count > 0){
            break;
        }
    }
}

return $count;
}

$any=BadWordFilter($qtitle,0);
$any=BadWordFilter($qtitle,1);

now to load the badwords from txt file I am using the following in place of

$patterns = array(
        '/butt/i',
        '/poop/i',
        '/crap/i'
    );

Following

$file_array = file('/path/to/badword.txt'); 
$patterns = array(); 
foreach ($file_array as $word_combo) 
{ 
$patterns[] = explode(',', $word_combo); 

} 

but it gave me delimiter error

Warning: preg_match() expects parameter 1 to be string, array given in myfile.php on line 60

Warning: preg_match() expects parameter 1 to be string, array given in myfile.php on line 60

Warning: preg_match() expects parameter 1 to be string, array given in myfile.php on line 60

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in myfile.php on line 57

Warning: preg_match() expects parameter 1 to be string, array given in myfile.php on line 60

Warning: preg_match() expects parameter 1 to be string, array given in myfile.php on line 60

Warning: preg_match() expects parameter 1 to be string, array given in myfile.php on line 60

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in myfile.php on line 57

line 60--$count = preg_match($pattern, $text);
line 57--$text = preg_replace($patterns, $replaces, $text, -1, $count);

Edited by ska_defender

4
Contributors
6
Replies
9
Views
5 Years
Discussion Span
Last Post by diafol
2

I am not a PHP programmer (then why am I on PHP forum?), but I can see that the error message means that the function expects individual strings instead of array of strings.

So you have to loop through each word in the text and replace it separately (if PHP has something like == for strings) or you have to concatenate the text array into one string

for_each($text as $word) $singleword += $word; //note this is not real PHP code

You also have to check each pattern separetely.

Votes + Comments
:)
0

Or instead of making it into one string it would be more effective to loop through individual word:

for_each($text as $word) {
   switch($word) {
      case "butt":
        $word = "b***";
        break;
      case "poop":
        $word = "p***";
        break;
   }
}

Lucky for me, PHP is a lot like C and C++ ;)

Edited by ModernC++

0

You can also try this solution:

<?php

function badword($word,$blacklist)
{
    if(in_array($word,$blacklist))
    {
        $w = substr($word,0,1);
        $len = strlen($word)-1;
        for($i=0;$i<$len;$i++)
        {
            $w .= '*';
        }
    }

    else
    {
        $w = $word;
    }

    return $w;
}

$blacklist = array('hello','world','bad','word');
$word = 'world';

echo badword($word,$blacklist); # output: w****

?>

$blacklist can be loaded from a database or from a text file if you use unserialize() or json_decode() The important is that you serve the black list as array. Bye!

Edited by cereal

0

cereal
can u please give me an example to use txt file instead of array
for example in your above code ?

0

Just replace $blacklist variable at line 23 of my example, if you want to use a csv/txt file where you store the bad words in this format:

hello, world, bad, word

then use:

$blacklist = array_map('trim',explode(',',file_get_contents('blacklist.txt')));

and you are finished. If you want to use json, instead, an example of the file contents will be:

["hello"," world"," bad"," word"]

and the $blacklist to use:

$blacklist = array_map('trim',json_decode(file_get_contents('blacklist.json')));

I hope it is clear enough.

1

For the replacement, you could do this:

function stars($item){
    return substr($item,0,1) . str_repeat('*',strlen($item)-1); 
}

$s = array('shoot','poop','dood');
$r = array_map('stars',$s);
$x = "Shoot the POOPy people";

echo str_ireplace($s,$r,$x);

For the writing to bad word array, it depends on the file structure. A csv type file: see cereal's last post.

Votes + Comments
neat!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.