Jump to content


Photo

need help with lang filter


  • Please log in to reply
6 replies to this topic

#1 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 11 October 2006 - 10:35 PM

Hi i got this code from the freaks tutorial section. How can i modify it so that it gets the obsenities from a mysql database without losing the ability to filter ie *** multiple curse words in a single input eg buttsagass should give ****sag***

<?php 
function language_filter($string) { 
    $obscenities = @file("path/to/your/file/foul_language.txt"); 
    foreach ($obscenities as $curse_word) { 
        if (stristr(trim($string),$curse_word)) { 
            $length = strlen($curse_word); 
            for ($i = 1; $i <= $length; $i++) { 
                $stars .= "*"; 
            } 
            $string = eregi_replace($curse_word,$stars,trim($string)); 
            $stars = ""; 
        } 
    } 
    return $string; 
} 
?> 


#2 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 12 October 2006 - 06:27 AM

You can try this.  Need to set the database info.
<?php 
function language_filter($string)
{
	// Connect to the database
	$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
	if (!$link)
		die("Could not connect to the database\n" . mysql_error());

	// Select the database
	$db_selected = mysql_select_db('database_name', $link);
	if (!$db_selected)
		die("Can't select database\n" mysql_error());

	// Select the words to filter
	$request = mysql_query("SELECT colName FROM tblName");

	// Define $obscenities and loop through the results and assign it to $obscenities
	$obscenities = array();
	while ($row = mysql_fetch_assoc($request))
		$obscenities[] = $row['colName'];

	// Free result abd close the connection
	mysql_free_result($request);
	mysql_close($link);

	foreach ($obscenities as $curse_word)
	{
		if (stristr(trim($string),$curse_word))
		{
			$length = strlen($curse_word);
			$stars = '';
			for ($i = 1; $i <= $length; $i++)
				$stars .= '*';

			$string = eregi_replace($curse_word, $stars, trim($string));
			$stars = '';
		}
	}

	return $string;
} 
?>

JayBachatero
SMF Developer && Converter Specialist

#3 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 12 October 2006 - 04:17 PM

Something went wrong it does retrieve the info from the mysql database but it only replaces one curse word eg. assmanbutt should return ***man**** but it only returns assman**** do u have any ideas on how to fix this problem

#4 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 12 October 2006 - 05:44 PM

I just tested it locally and it worked.  Check to see that you actually have that word in the database.
JayBachatero
SMF Developer && Converter Specialist

#5 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 12 October 2006 - 08:12 PM

thanks i finally got it to work the way i want it to but i still have one more problem. How can i save the name of each curse_word replaced with *** in an array.

#6 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 12 October 2006 - 11:12 PM

You want to log each of the curse word?
JayBachatero
SMF Developer && Converter Specialist

#7 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 13 October 2006 - 02:12 AM

Yes, basically




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users