Clase en php para filtrar texto

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.

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.
Sin votos aún

Enviar un comentario nuevo

El contenido de este campo se mantiene privado y no se mostrará públicamente.
  • Las direcciones de las páginas web y las de correo se convierten en enlaces automáticamente.
  • Etiquetas HTML permitidas: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Saltos automáticos de líneas y de párrafos.

Más información sobre opciones de formato

CAPTCHA
Esta pregunta es para probar si tu eres un humano y para prevenir spam en el sitio.
6 + 13 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.