stawkerm0h Posted March 22, 2011 Share Posted March 22, 2011 i currently have this code...i got a problem calling out words from my database....my database name is "bad_replace" and the list of my bad words is "word"....my codes only pick out some of my bad words out of database and not all what i declared on my database....like when i post the word "motherfucker" it becomes "mother******" but if i post "tanga" which a bad word from our country it doesn't replace into "*****" since that word is also on my database.......please help... thanks..... <?php include('connect.php'); function swear_filter($string) { $bad_words = array(); $sql = mysql_query("SELECT * FROM bad_replace"); if (mysql_num_rows($sql) > 0) { while ($row = mysql_fetch_assoc($sql)) { $bad_words[] = '/\b' . $row['word'] . '\b/i'; $replacement[] =$row['replacement']; } } } $string_to_check = ""; $completed_filter = ""; $finished_filter = ""; if ($_POST['submit']) { echo swear_filter($_POST['string']); } ?> <hr /> <center><form action='swear_filter.php' method='POST'> <br><textarea name='text' rows="10" cols="75></textarea><br /> <input type='submit' name='submit' value='Post'></p></center> </form> Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/ Share on other sites More sharing options...
darkfreaks Posted March 22, 2011 Share Posted March 22, 2011 this class may be more useful than a function that doesn't match both case sensitive and insensitive words. <?php class BadWordDetector { // Properties protected $badwords = array(); /*********************************************************************** Class Constructor ***********************************************************************/ public function __construct($badwords) { //convert $badwords values to lower case for case-insensitive comparisons $this->setBadWords($badwords); } /*********************************************************************** Class Accessor Methods ***********************************************************************/ public function getBadWords() { return $this->badwords; } // function to reset the $badwords array to new values public function setBadWords($badwords) { foreach($this->badwords as $i => $value) { unset($this->badwords[$i]); } //convert $badwords values to lower case for case-insensitive comparisons foreach($badwords as $i => $value) { $this->badwords[$i] = strtolower($value); } } /*********************************************************************** Class Methods ***********************************************************************/ public function cleanString($str) { $msgWords = explode(' ',$str); //split the elements with punct. characters and expand $msgWords array $msgWordsExpand = array(); foreach($msgWords as $i => $currWord) { if(!eregi("([[:punct:]])$",$currWord)) { $msgWordsExpand[] = ' '.$currWord; } else { //there is a punctuation mark at the end of the word, so split it out $str1 = substr($currWord,0,strlen($currWord)-1); $str2 = substr($currWord,strlen($currWord)-1); array_push($msgWordsExpand,' '.$str1,$str2); } } //create a clean string from $msgWordsExpand $strClean = ''; foreach($msgWordsExpand as $i => $currWord) { $strClean = in_array(trim(strtolower($currWord)),$this->badwords)? $strClean.' ****': $strClean.$msgWordsExpand[$i]; } return $strClean; } } //end of class //------------------------------------------------------------ //testing code $badwords = array('Amet','tinCidunt','congue'); $message = 'Lorem ipsum dolor sit ameT, consectetur adipiscing elit. Mauris tincidunt auctor ligula, sed conGue mauris gravida gravida. '; $detector = new BadWordDetector($badwords); echo '<br /><br />'.$message.'<br />'.$detector->cleanString($message); //change the set of bad words $badwords = array('Amet'); $detector->setBadWords($badwords); echo '<br /><br />'.$message.'<br />'.$detector->cleanString($message); ?> Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/#findComment-1190640 Share on other sites More sharing options...
stawkerm0h Posted March 22, 2011 Author Share Posted March 22, 2011 i got an error at line 36... it says "Deprecated: Function eregi() is deprecated in C:\xampp\htdocs\try2.php on line 36" and anyone who can help me to correct MY codes.... darkfreaks, i can't understand your codes i'm just starting studying php... so if you can...just help me to correct MY codes and thank you for reply please help Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/#findComment-1190724 Share on other sites More sharing options...
darkfreaks Posted March 22, 2011 Share Posted March 22, 2011 Eregi has been deprecated in php versions 5.3.0 and higher it is reccomended not to use it. might try preg_match or stristr Instead. Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/#findComment-1190938 Share on other sites More sharing options...
sasa Posted March 22, 2011 Share Posted March 22, 2011 change your function to function swear_filter($string) { $bad_words = array(); $sql = mysql_query("SELECT * FROM bad_replace"); if (mysql_num_rows($sql) > 0) { while ($row = mysql_fetch_assoc($sql)) { $bad_words[] = '/\b' . $row['word'] . '\b/i'; $replacement[] = $row['replacement']; } } return preg_replace($bad_words, $replacement, $string); } Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/#findComment-1190941 Share on other sites More sharing options...
stawkerm0h Posted March 23, 2011 Author Share Posted March 23, 2011 what i want to do is that only root words is on my database... but when i type "superfuck" and the word "fuck" is on my database it will be filtered into "super****" or "motherfuck" will be filtered into "mother****"....anyone could help me in coding?? Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/#findComment-1191542 Share on other sites More sharing options...
stawkerm0h Posted March 25, 2011 Author Share Posted March 25, 2011 anyone could help me? Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/#findComment-1192137 Share on other sites More sharing options...
sasa Posted March 25, 2011 Share Posted March 25, 2011 in that case change to function swear_filter($string) { $bad_words = array(); $sql = mysql_query("SELECT * FROM bad_replace"); if (mysql_num_rows($sql) > 0) { while ($row = mysql_fetch_assoc($sql)) { $bad_words[] = '/' . $row['word'] . '/i'; $replacement[] = $row['replacement']; } } return preg_replace($bad_words, '*****', $string); } Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/#findComment-1192177 Share on other sites More sharing options...
stawkerm0h Posted March 26, 2011 Author Share Posted March 26, 2011 thanks sir sasa... it work perfectly......... and you save us because it's our thesis.... but one more thing sir sasa on our topic title since our thesis is a forum that filter bad words can you configure my code so that my topic title can also filter "motherfucker" into "mother******" here's my code sir sasa.... echo "<tr><td valign='top' style='border: 2px solid #000000;'><div style='min-height: 100px;'>".filter_words($row['topic_title'])."<br />by ".$row2['post_creator']." - ".$row2['post_date']."<hr />".filter_words($row2['post_content'])."</div></td><td width='200' valign='top' align='center' style='border: 2px solid #6F0000;'>User Info Here</td></tr><tr><td colspan='2'><hr /></td></tr>"; this line filter our post content ".filter_words($row2['post_content'])." but how in our topic title...because i tried that my topic title would be motherfucker and it's not filtering ".filter_words($row['topic_title'])." can you help me again sir sasa thanks Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/#findComment-1192456 Share on other sites More sharing options...
stawkerm0h Posted March 26, 2011 Author Share Posted March 26, 2011 thanks sir sasa i already figure out how to filter the topic title... thanks a lot sir sasa Quote Link to comment https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/#findComment-1192457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.