Código:
/**
* WordFilter
* Class for censoring words in text
* @access public
* @package SPLIB
*/
class WordFilter {
/**
* An array of words to censor
* @access private
* @var array
*/
var $badWords;
/**
* WordFilter constructor
* Randomly generates strings to censor words with
* @param array an array of words to filter
* @access public
*/
function WordFilter($badWords)
{
$this->badWords = array();
srand((float)microtime() * 1000000);
$censors = array('$', '@', '#', '*');
foreach ($badWords as $badWord) {
$badWord = preg_quote($badWord);
$replaceStr = '';
$size = strlen($badWord);
for ($i = 0; $i < $size; $i++) {
shuffle($censors);
$replaceStr .= $censors[0];
}
$this->badWords[$badWord] = $replaceStr;
}
}
/**
* Searches for bad words in text and censors them
* @param string text to filter
* @return string the filtered text
* @access public
*/
function filter($text)
{
foreach ($this->badWords as $badWord => $replaceStr) {
$text = preg_replace('/' . $badWord . '/i', $replaceStr,
$text);
}
return $text;
}
}
// An array of words to replace
$badWords = array('tubby', 'tubbies', 'byebye');
// Include the word file with the list of bad words
$wordFilter = new WordFilter($badWords);
// $text simulates some data from the database
$text = 'byebye!';
// Filter the words
$text = $wordFilter->filter($text);
echo $text;
?>
Fuente: Libro The PHP Anthology, Volumen II.
No hay comentarios:
Publicar un comentario