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
https://forums.phpfreaks.com/topic/257495-not-filtering-words/
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
https://forums.phpfreaks.com/topic/257495-not-filtering-words/#findComment-1319778
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.