REVISED FROM OLD QUESTION- NOW USING DIFFERENT WAY FORWARD
I am building a chatbox and would like to use emoticons. So when one sends ” 🙂 ” it is replaced by and image of it, unfortunately with my code this doesn’t happen. I have done quite a bit of experimentation too, I’ve learned str_replace will achive my project goose eggs, and I have decied to use preg_replace
<?php
$message = $chatText;
$emoticons = array(
"/\:\)/");
$replacements = array(
"<img src='images/icons/smileys/smile.png' width='20' height='20' alt='Smile' />");
$chatText = preg_replace($emoticons,$replacements,$message);
?>
Instead of replacing the “:)” with the smiley face, it just inserts the text <img src='images/icons/smileys/smile.png' width='20' height='20' alt='Smile' />
This is not a problem with your regex, rather an issue with how you are displaying the data. At some point you are using something like
htmlspecialcharsto prevent chatters from injecting HTML into your chat window. Unfortunately this results in smileys being escaped too.Your code for replacing smileys should come somewhere after escaping the original text.