Jump to content

[SOLVED] building an array with mysql results, then parsing them


scarhand

Recommended Posts

I'm trying to set up a word censor for a script I'm working on.

 

Heres the script (please ignore the smiley parser, that works perfect):

 

<?php
$censors_query = "SELECT * FROM censors";
$censors_result = mysql_query($censors_query);
$bwn = 0;

while($row = mysql_fetch_assoc($censors_result))
{
  $badwords[$bwn] = $row['word'];
  $bwn++;
}


class parser 
{ 
  var $parse_smilies;
  var $smiley_url;
  var $parse_censors;
   
  function parser() 
  { 
    global $smilies;
    
    if ($smilies == 'yes') {
      $smilies = 'true';
     }
    else {
      $smilies = 'false';
    }
    
    $this->parse_smilies = $smilies; 
    $this->smiley_url    = "/smileys"; 
    $this->parse_censors = true;
  } 
   
  function parse($data) 
  { 
    $data = htmlentities($data); 
     
    if($this->parse_smilies) 
    { 
        $data = $this->_parse_smilies($data); 
    } 
    
    if($this->parse_censors) 
    { 
        $data = $this->_parse_censors($data); 
    } 
     
    return $data; 
  } 
   
  function _parse_smilies($data) 
  { 
    $smilies = array( 
        '' => 'smile.gif', 
        '' => 'sad.gif', 
      ); 
     
    if(substr($this->smiley_url,-1) != '/' && !empty($this->smiley_url)) 
    { 
      $this->smiley_url .= '/'; 
    } 
     
    foreach($smilies as $code => $image) 
    { 
      $data = str_replace($code, "<img src='{$this->smiley_url}{$image}' alt='{$code}' />", $data); 
    } 

    return $data; 
  } 
  
  function _parse_censors($data) 
  {
    global $badwords;
    $mask = "*";
    
    for ($i = 0; $i < sizeof($words); $i++) 
    {
      $censored = substr($badwords[$i], 0, 1);
      
      for ($x = 1; $x < strlen($badwords[$i]); $x++) $censored .= $mask;
      $data = str_replace($badwords[$i], $censored, $data);
    }
    
    return $data;
  }
} 
?>

 

Any help would be greatly appreciated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.