0

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.

1
  • If you're not sure yet which tutorial that is going to be, you should try this one. Commented Aug 23, 2013 at 9:19

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed...I didn't realize I could use this :) Thanks! I did however needed to change '/:(/'; into ':('; to make it work with str_replace.
3

Just escape the parenthesis:

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

and also, change the last line to:

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

1 Comment

Thanks for your suggestion! However the other answer is better in my situation.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.