Clase en php para filtrar texto
Published by danilo04 on April 2nd, 2008 in Uncategorized
Cuando creamos un foro por ejemplo nos vemos en la necesidad de crear filtros para malas palabras. La siguiente clase se encarga de filtrar texto dependiendo de un arreglo de palabras que estén definidas con anterioridad.
Fuente: Libro The PHP Anthology, Volumen II.
Código:<span style="color: rgb(0, 0, 0);"> <span style="color: rgb(0, 0, 187);"><?php<br /><br /></span><span style="color: rgb(255, 128, 0);">/**<br /> * WordFilter<br /> * Class for censoring words in text<br /> * @access public<br /> * @package SPLIB<br /> */<br /></span><span style="color: rgb(0, 119, 0);">class </span><span style="color: rgb(0, 0, 187);">WordFilter </span><span style="color: rgb(0, 119, 0);">{<br /> </span><span style="color: rgb(255, 128, 0);">/**<br /> * An array of words to censor<br /> * @access private<br /> * @var array<br /> */<br /> </span><span style="color: rgb(0, 119, 0);">var </span><span style="color: rgb(0, 0, 187);">$badWords</span><span style="color: rgb(0, 119, 0);">;<br /> </span><span style="color: rgb(255, 128, 0);">/**<br /> * WordFilter constructor<br /> * Randomly generates strings to censor words with<br /> * @param array an array of words to filter<br /> * @access public<br /> */<br /> </span><span style="color: rgb(0, 119, 0);">function </span><span style="color: rgb(0, 0, 187);">WordFilter</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$badWords</span><span style="color: rgb(0, 119, 0);">)<br /> {<br /> </span><span style="color: rgb(0, 0, 187);">$this</span><span style="color: rgb(0, 119, 0);">-></span><span style="color: rgb(0, 0, 187);">badWords </span><span style="color: rgb(0, 119, 0);">= array();<br /> </span><span style="color: rgb(0, 0, 187);">srand</span><span style="color: rgb(0, 119, 0);">((float)</span><span style="color: rgb(0, 0, 187);">microtime</span><span style="color: rgb(0, 119, 0);">() * </span><span style="color: rgb(0, 0, 187);">1000000</span><span style="color: rgb(0, 119, 0);">);<br /> </span><span style="color: rgb(0, 0, 187);">$censors </span><span style="color: rgb(0, 119, 0);">= array(</span><span style="color: rgb(221, 0, 0);">'$'</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(221, 0, 0);">'@'</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(221, 0, 0);">'#'</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(221, 0, 0);">'*'</span><span style="color: rgb(0, 119, 0);">);<br /> foreach (</span><span style="color: rgb(0, 0, 187);">$badWords </span><span style="color: rgb(0, 119, 0);">as </span><span style="color: rgb(0, 0, 187);">$badWord</span><span style="color: rgb(0, 119, 0);">) {<br /> </span><span style="color: rgb(0, 0, 187);">$badWord </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">preg_quote</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$badWord</span><span style="color: rgb(0, 119, 0);">);<br /> </span><span style="color: rgb(0, 0, 187);">$replaceStr </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(221, 0, 0);">''</span><span style="color: rgb(0, 119, 0);">;<br /> </span><span style="color: rgb(0, 0, 187);">$size </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">strlen</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$badWord</span><span style="color: rgb(0, 119, 0);">);<br /> for (</span><span style="color: rgb(0, 0, 187);">$i </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$i </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">$size</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$i</span><span style="color: rgb(0, 119, 0);">++) {<br /> </span><span style="color: rgb(0, 0, 187);">shuffle</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$censors</span><span style="color: rgb(0, 119, 0);">);<br /> </span><span style="color: rgb(0, 0, 187);">$replaceStr </span><span style="color: rgb(0, 119, 0);">.= </span><span style="color: rgb(0, 0, 187);">$censors</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">];<br /> }<br /> </span><span style="color: rgb(0, 0, 187);">$this</span><span style="color: rgb(0, 119, 0);">-></span><span style="color: rgb(0, 0, 187);">badWords</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(0, 0, 187);">$badWord</span><span style="color: rgb(0, 119, 0);">] = </span><span style="color: rgb(0, 0, 187);">$replaceStr</span><span style="color: rgb(0, 119, 0);">;<br /> }<br /> }<br /> </span><span style="color: rgb(255, 128, 0);">/**<br /> * Searches for bad words in text and censors them<br /> * @param string text to filter<br /> * @return string the filtered text<br /> * @access public<br /> */<br /> </span><span style="color: rgb(0, 119, 0);">function </span><span style="color: rgb(0, 0, 187);">filter</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$text</span><span style="color: rgb(0, 119, 0);">)<br /> {<br /> foreach (</span><span style="color: rgb(0, 0, 187);">$this</span><span style="color: rgb(0, 119, 0);">-></span><span style="color: rgb(0, 0, 187);">badWords </span><span style="color: rgb(0, 119, 0);">as </span><span style="color: rgb(0, 0, 187);">$badWord </span><span style="color: rgb(0, 119, 0);">=> </span><span style="color: rgb(0, 0, 187);">$replaceStr</span><span style="color: rgb(0, 119, 0);">) {<br /> </span><span style="color: rgb(0, 0, 187);">$text </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">preg_replace</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'/' </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(0, 0, 187);">$badWord </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(221, 0, 0);">'/i'</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$replaceStr</span><span style="color: rgb(0, 119, 0);">,<br /> </span><span style="color: rgb(0, 0, 187);">$text</span><span style="color: rgb(0, 119, 0);">);<br /> }<br /> return </span><span style="color: rgb(0, 0, 187);">$text</span><span style="color: rgb(0, 119, 0);">;<br /> }<br /> }<br /><br /></span><span style="color: rgb(255, 128, 0);">// An array of words to replace<br /></span><span style="color: rgb(0, 0, 187);">$badWords </span><span style="color: rgb(0, 119, 0);">= array(</span><span style="color: rgb(221, 0, 0);"></span><span style="color: rgb(0, 119, 0);"></span><span style="color: rgb(221, 0, 0);">'tubby'</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(221, 0, 0);">'tubbies'</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(221, 0, 0);">'byebye'</span><span style="color: rgb(0, 119, 0);">);<br /></span><span style="color: rgb(255, 128, 0);">// Include the word file with the list of bad words<br /></span><span style="color: rgb(0, 0, 187);">$wordFilter </span><span style="color: rgb(0, 119, 0);">= new </span><span style="color: rgb(0, 0, 187);">WordFilter</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$badWords</span><span style="color: rgb(0, 119, 0);">);<br /></span><span style="color: rgb(255, 128, 0);">// $text simulates some data from the database<br /></span><span style="color: rgb(0, 0, 187);">$text </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(221, 0, 0);">'byebye!'</span><span style="color: rgb(0, 119, 0);">;<br /></span><span style="color: rgb(255, 128, 0);">// Filter the words<br /></span><span style="color: rgb(0, 0, 187);">$text </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$wordFilter</span><span style="color: rgb(0, 119, 0);">-></span><span style="color: rgb(0, 0, 187);">filter</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$text</span><span style="color: rgb(0, 119, 0);">);<br />echo </span><span style="color: rgb(0, 0, 187);">$text</span><span style="color: rgb(0, 119, 0);">;<br /></span><span style="color: rgb(0, 0, 187);">?></span> </span>
Fuente: Libro The PHP Anthology, Volumen II.

Enviar un comentario nuevo