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

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.

commented: :) +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++ ;)

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!

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

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.

Member Avatar for diafol

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.

commented: neat! +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.