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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.