Read 282 times | Created 2013-01-26 20:18:24 | Updated 2013-01-26 20:18:24 | | |

 

<?php
/*********************************
FILENAME  : filter.php
CREATE BY  : cahya dsn
PURPOSE   : filtering table
CREATE DATE : 2013-01-27
*********************************/
/*
#table creation
use test;
DROP TABLE IF EXISTS `tbfilter`;
CREATE TABLE IF NOT EXISTS `tbfilter` (
  `kata` varchar(30) NOT NULL,
  UNIQUE KEY `kata` (`kata`)
) ENGINE=MyISAM;
INSERT INTO `tbfilter`(`kata`) VALUES
('ada'),('adalah'),('mereka');

DROP TABLE IF EXISTS `tbtampung`;
CREATE TABLE IF NOT EXISTS `tbtampung` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
  `kalimat` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;

*/
//database configuration
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='test';
//database connection
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
//query to get filtering words data from database 
$sql="SELECT * FROM tbfilter";
$result=$db->query($sql);
//variables initialization
$filter=array();
//generate filtering words datas from tbfilter table
// 1. Fetch data from database
while($records=$result->fetch_array()){
  $filter[]="/{$records[0]}|/";
}
$result->close();
// 2. get input text to be filtering (e.g from input form)
$input="mereka adalah orang pintar";
// 3. preparing input string
$input=preg_replace("/s/","|",$input);
// 4. filtering input text to get clean output 
$output=trim(preg_replace('!s+!', ' ',str_replace("|"," ",preg_replace($filter, '', $input))));
// 5. build query to store output text to tbtampung table
$sql="INSERT INTO tbtampung(`id`,`kalimat`)VALUES(NULL,'$output')";
// 6.execute the query
$db->query($sql);
$db->close();
echo $output;
?>