Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/231336-php-and-mysql-probelm/
Share on other sites

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);

?> 

 

 

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

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);
}

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

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);
} 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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