Basic word filter/replace words ::Class::

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Basic word filter/replace words ::Class::

Post by dzcadii » Sun Mar 01, 2015 6:33 pm

Here is a simple script that will replace any defined words in a database.

This script reads the input, checks the database for that word and replaces it with the instructed replacement.

Database set up:
Database table name wordfilter
id int(11) autoincrement primary
word varchar(255)
replace_with varchar(255)

Code: Select all

<?
/*****************************
     filter.php
*****************************/
//$UserInput['comments'] would be the $_POSTED comment from the form

class word_filter{

function word_filter($UserInput){
//Connect to the database
$this->db_connection();

$this->query_string = "SELECT * FROM wordfilter";
$this->query = mysql_query($this->query_string);

$PWords=array();//Post words
$PWordsR=array();//Replacements

while($WF=mysql_fetch_assoc($this->query)){
extract($WF);
$PWords[]=$word;
$PWordsR[$word]=$replace_with;
}
foreach($PWords as $key=>$value){
$StripT=strip_tags($UserInput['comments']);
$Replace=str_ireplace($PWords,$PWordsR,$StripT);
}
$this->NUserInput=$Replace;

}

function db_connection(){
//Put your database connection functions and information here
//To include selecting the wordfilter database table
}
}
?>
This is how you would call the filter

Code: Select all

<?
$filter = new filter($UserInput);
//Echo out the new result (this includes the changes)
echo $filter->NUserInput;
?>

Post Reply