Jump to content

Not filtering words?


3raser

Recommended Posts

I'm pretty sure the problem is starring me in the face, but I can't seem to locate it.

 

function filter($string)
{
//swear words pulled from bannedwordlist.com
$f = fopen('../badwords.txt', 'r');
$bad_words = fread($f, filesize('../badwords.txt'));
$bad_words = explode('\n', $bad_words);

$input = strtolower($string);

foreach($bad_words as $value)
{
	$string = str_replace($value, '****', $input);
}

return $string;
}

 

UPDATED CODE

function filter($string)
{
//swear words pulled from bannedwordlist.com
$f = fopen('../badwords.txt', 'r');
$bad_words = fread($f, filesize('../badwords.txt'));
$bad_words = explode('\n', $bad_words);

$input = strtolower($string);
$string = str_replace($bad_words, '****', $input);

return $string;
}

 

I've already echoed out $value in the foreeach loop, and it does correctly retrieve the bad words and put them into an array. My only problem is, the returned string is still in strtolower() form, and the words aren't censored. :/

Link to comment
Share on other sites

Since strtolower() isn't always reversible, you'd be better off using str_ireplace

 

Also, Windows (\r\n) handles line-breaks differently than other operating systems (\n only), so if you explode by \n and your lines are terminated with \r\n, you will end up with "curseword\r" array values.

 

This example works properly

<?php

$search = array('one','two','three');
$body = 'This one, those two, make three';

$body = str_ireplace($search, '***', $body);

echo $body;

?>

so I'm going to assume it's a problem with your array values.

Link to comment
Share on other sites

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.