Having trouble with Matching String

Hi guys, I was advise in this community to use regex in adding smilies in viewing a message. However I am stuck. Can anyone explain what is wrong with my code espcially if my IF statement in preg_match statement. Here is my code


$query = "SELECT CONCAT(SUBSTR(users.firstname,1,1),'. ',users.lastname) AS 'user', reply, datereplied FROM
                    users INNER JOIN replymsg ON replymsg.repliedby=users.id WHERE replymsg.msgid='$id'";
          
          $result = mysql_query($query);
          if(mysql_num_rows($result)>0)
          {
            while($entry=mysql_fetch_row($result))
            {
              echo "<div class='title'>::Replied by: $entry[0] | $entry[2]::</div>";
              
              $message = $entry[1];
              $query = "SELECT id, filename FROM smilies";
              $smilies = mysql_query($query);
              while($icons = mysql_fetch_row($smilies))
              {
                if(preg_match($icons[0],$message))
                {
                  $msg = str_replace($message,"<img src=./images/".$icons[1], $msg);
                }
              }
              
              //echo "<div style='width: 440px'>$entry[1]</div><br />";
              echo "<div style='width: 440px'>$msg</div><br />";
            }
          }

In my database, my id has something like

:injured:

and of course in the filename it has a value of injured.gif.

Why is it that it does’nt display the smilies on my message?

I see no need for RegExp here, a simple str_replace should suffice. :slight_smile:


<?php
while($smiley = mysql_fetch_assoc($result)){
    $message = str_replace(
        $smiley['bbcode'],
        sprintf(
            '<img src="/assets/images/&#37;s" alt="%s" />',
            $smiley['image'],
            $smiley['bbcode']
        ),
        $message
    );
}
?>

Are you sure? What is the resulting HTML code?

well, in my messageboard I have this output.
I am (sad) and you are (cool).

Well, if you have things like :injured: in your database, and (injured) in your message, then it’ll never work :slight_smile:

But the problem in your code are the parameters you pass to str_replace. Check out the manual, and Anthony’s code.

I have an ajax chat room script that I’m developing, and it has a function to parse emoticons, as well. My function is quite different from yours, but it may prove useful:


  function parseEmotes($msg) {
    global $lastFunction, $smilieArray;                                 // Load the emoticon library
    rsort($smilieArray);
    $lastFunction .= "_parseEmotes";
    $emotesPath = "./emotes";                                           // Define the location of the emote image
    foreach ($smilieArray as $line) {                                   // Iterate through the list
      $line = rtrim($line);                                             // Trim off excess line feeds and whitespace
      list($symbol, $fname, $width, $height, $alt) = split(", ",$line); // Seperate the various values from the list
      $alt = rtrim($alt);                                               // Just to make sure the line is clean
      $tmpAlt = substr($alt,0,1) . '&#' . ord(substr($alt,1,1)) . substr($alt,2);
      if (strpos($msg,$symbol) !== false) {                             // If it finds an emoticon symbol, then process it
        $emot = <<<endLine
<img src="$emotesPath/$fname" width="$width" height="$height" alt="$tmpAlt" title="$tmpAlt" />

endLine;
        $msg = str_replace($symbol, $emot, $msg);                       // swap out the found symbol for it's emote image
      }
    }
    return $msg;                                                        // Send back the processed image
  }
// end function parseEmotes

And here are a few lines of the data file for the emotes:


;), smile3.gif, 15, 15, ;)
:(, smile2.gif, 15, 15, :(
:x, kiss.gif, 19, 19, :x
:P, smile4.gif, 15, 15, :P
:*, star.gif, 25, 25, :*
:,(, cry.gif, 21, 16, :,(
8), cool.gif, 17, 17, 8)

I know that the first and last parts are redundant. There was good reason for it, back when I first started this script, and I’m too lazy to change it now. :smiley: