Nick
Nick

Reputation: 169

PHP preg_replace strings to images

When a user wants to send a message, he can use emoticons. What happens, is that a users clicks on an emoticon, so that it inserts the corresponding text, like this:

:D

Now, after the message has been sent, the other person wants to see it. What I want is to replace the :D with an image...

Here is what I got:

  $patterns = array();
  $patterns[0] = '/:)/';
  $patterns[1] = '/:(/';
  $patterns[2] = '/:D/';
  $patterns[3] = '/:C/';
  $patterns[4] = '/:A/';
  $patterns[5] = '/:H/';
  $patterns[6] = '/:L/';
  $patterns[7] = '/:O/';
  $patterns[8] = '/:S/';
  $patterns[9] = '/;)/';
  $replacements = array();
  $replacements[0] = '<img alt=":)" border="0" src="./images/smileys/happy.png" width="25px" />';
  $replacements[1] = '<img alt=":(" border="0" src="./images/smileys/sad.png" width="25px" />';
  $replacements[2] = '<img alt=":D" border="0" src="./images/smileys/veryhappy.png" width="25px" />';
  $replacements[3] = '<img alt=":C" border="0" src="./images/smileys/cry.png" width="25px" />';
  $replacements[4] = '<img alt=":A" border="0" src="./images/smileys/angry.png" width="25px" />';
  $replacements[5] = '<img alt=":H" border="0" src="./images/smileys/heart.png" width="25px" />';
  $replacements[6] = '<img alt=":L" border="0" src="./images/smileys/love.png" width="25px" />';
  $replacements[7] = '<img alt=":O" border="0" src="./images/smileys/nothing.png" width="25px" />';
  $replacements[8] = '<img alt=":S" border="0" src="./images/smileys/scared.png" width="25px" />';
  $replacements[9] = '<img alt=";)" border="0" src="./images/smileys/wink.png" width="25px" />';
  preg_replace($patterns, $replacements, $bericht);

But these aren't the right Regexes...so I get a php error. In the near future I'd like to take a tutorial on how regexes work, but I hope someone can help me out now.

Is there a simple and quick way to do this? Preferably per pattern like I do now, not in 1 big regex.

Upvotes: 0

Views: 282

Answers (3)

Spudley
Spudley

Reputation: 168655

You're replacing fixed strings, so there's absolutely no need to use regex patterns here at all; a straightforward str_replace() will do just fine.

Or better yet, use strtr(), because then you can use an associative array for your replacement data...

$replacements = array(
    ':)' = '<img alt=":)" border="0" src="./images/smileys/happy.png" width="25px" />',
    ':(' = '<img alt=":(" border="0" src="./images/smileys/sad.png" width="25px" />',
    ':D' = '<img alt=":D" border="0" src="./images/smileys/veryhappy.png" width="25px" />',
    ':C' = '<img alt=":C" border="0" src="./images/smileys/cry.png" width="25px" />',
    ':A' = '<img alt=":A" border="0" src="./images/smileys/angry.png" width="25px" />',
    ':H' = '<img alt=":H" border="0" src="./images/smileys/heart.png" width="25px" />',
    ':L' = '<img alt=":L" border="0" src="./images/smileys/love.png" width="25px" />',
    ':O' = '<img alt=":O" border="0" src="./images/smileys/nothing.png" width="25px" />',
    ':S' = '<img alt=":S" border="0" src="./images/smileys/scared.png" width="25px" />',
    ';)' = '<img alt=";)" border="0" src="./images/smileys/wink.png" width="25px" />',
);

$output = strtr($input, $replacements);

Much simpler!

One thing you should watch out for is inadvertently replacing strings that aren't intended to be emoticons. This is quite a common problem for programs that do this kind of thing. (I've been caught out by Skype plenty of times converting things like (c) and 8) into icons when I didn't intend it to)

Upvotes: 0

pauk960
pauk960

Reputation: 300

Since you don't need regex search patterns you can simply use str_replace.

From manual page:

If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of preg_replace().

$bericht = str_replace($patterns, $replacements, $bericht);

Upvotes: 3

Toto
Toto

Reputation: 91373

Just escape the parenthesis:

$patterns[0] = '/:\)/';
$patterns[1] = '/:\(/';
$patterns[9] = '/;\)/';

and also, change the last line to:

$bericht = preg_replace($patterns, $replacements, $bericht);

Upvotes: 3

Related Questions