oracle259 Posted September 26, 2006 Share Posted September 26, 2006 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.[code]<?phpfunction 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;}?> [/code] Link to comment https://forums.phpfreaks.com/topic/22164-mysql-language-filter-solved/ Share on other sites More sharing options...
sanfly Posted September 26, 2006 Share Posted September 26, 2006 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 Link to comment https://forums.phpfreaks.com/topic/22164-mysql-language-filter-solved/#findComment-99240 Share on other sites More sharing options...
oracle259 Posted September 26, 2006 Author Share Posted September 26, 2006 [quote]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[/quote]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. Link to comment https://forums.phpfreaks.com/topic/22164-mysql-language-filter-solved/#findComment-99246 Share on other sites More sharing options...
sanfly Posted September 26, 2006 Share Posted September 26, 2006 So whats your database structure like? Link to comment https://forums.phpfreaks.com/topic/22164-mysql-language-filter-solved/#findComment-99253 Share on other sites More sharing options...
oracle259 Posted September 26, 2006 Author Share Posted September 26, 2006 Pretty much this[code]$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());[/code] Link to comment https://forums.phpfreaks.com/topic/22164-mysql-language-filter-solved/#findComment-99255 Share on other sites More sharing options...
sanfly Posted September 26, 2006 Share Posted September 26, 2006 Give this a go, might be a bit buggy, obviously cant test it...[code=php:0]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;}[/code] Link to comment https://forums.phpfreaks.com/topic/22164-mysql-language-filter-solved/#findComment-99263 Share on other sites More sharing options...
oracle259 Posted September 26, 2006 Author Share Posted September 26, 2006 Unfortunately it did not work i think because of the missing foreach statement[code]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;}[/code] Link to comment https://forums.phpfreaks.com/topic/22164-mysql-language-filter-solved/#findComment-99283 Share on other sites More sharing options...
oracle259 Posted September 26, 2006 Author Share Posted September 26, 2006 Sorry my bad it worked great thanks alot. :) Link to comment https://forums.phpfreaks.com/topic/22164-mysql-language-filter-solved/#findComment-99284 Share on other sites More sharing options...
sanfly Posted September 26, 2006 Share Posted September 26, 2006 Your welcome ;DEdit your first post to put **SOLVED** in the title Link to comment https://forums.phpfreaks.com/topic/22164-mysql-language-filter-solved/#findComment-99306 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.