Jump to content


Photo

Mysql language filter *** SOLVED ***


  • Please log in to reply
8 replies to this topic

#1 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 26 September 2006 - 09:31 PM

How can i modify the language filter tutorial code to work with a mysql database instead of a flat text file. I have tried several times with no luck.

<?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 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 26 September 2006 - 09:38 PM

I guess you first need to decide how you will store the banned words in your database?  A table for banned words then each word stored as a separate row?  or a single row entry with all the words, separated by commas or line breaks or something like that.  Once you have decided that, we can move on to making a script to check
If you're not part of the solution, you're part of the precipitate

#3 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 26 September 2006 - 09:47 PM

I guess you first need to decide how you will store the banned words in your database?  A table for banned words then each word stored as a separate row?  or a single row entry with all the words, separated by commas or line breaks or something like that.  Once you have decided that, we can move on to making a script to check


I used the first approach.. which doesnt seem to be working too well. but thats the best way to keep adding to the database over time.

#4 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 26 September 2006 - 09:53 PM

So whats your database structure like?
If you're not part of the solution, you're part of the precipitate

#5 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 26 September 2006 - 09:57 PM

Pretty much this

$sql="CREATE TABLE `langfilter` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `word` varchar(225) NOT NULL default '',
  `tag` char(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM";
$result = dbQuery($sql) or do_error("Couldn't execute SQL: $sql". mysql_error());


#6 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 26 September 2006 - 10:15 PM

Give this a go, might be a bit buggy, obviously cant test it...

function language_filter($string) {
    $obsenities = mysql_query("SELECT * FROM langfilter") or die(mysql_error());
	while($row = mysql_fetch_array($obsenities)){
		$curse_word = $row['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;
}



If you're not part of the solution, you're part of the precipitate

#7 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 26 September 2006 - 10:37 PM

Unfortunately it did not work i think because of the missing foreach statement

function language_filter($string) {
    $obsenities = mysql_query("SELECT * FROM langfilter") or die(mysql_error());

	

while($row = mysql_fetch_array($obsenities)){

	

	

$curse_word = $row['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;
}


#8 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 26 September 2006 - 10:38 PM

Sorry my bad it worked great thanks alot.  :)

#9 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 26 September 2006 - 11:23 PM

Your welcome  ;D

Edit your first post to put **SOLVED** in the title
If you're not part of the solution, you're part of the precipitate




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users