Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:52:45+00:00 2026-05-28T02:52:45+00:00

I need to replace textual emoticons to html image tags. I compiled the following

  • 0

I need to replace textual emoticons to html image tags. I compiled the following data:

private $smile = array(">:]", ":-)", ":)", ":o)", ":]", ":3", ":c)", ":>", "=]", "8)", "=)", ":}", ":^)");
private $laugh = array(">:D", ":-D", ":D", "8-D", "x-D", "X-D", "=-D", "=D", "=-3", "8-)");
private $sad = array(">:[", ":-(", ":(",  ":-c", ":c", ":-<", ":-[", ":[", ":{", ">.>", "<.<", ">.<");
private $wink = array(">;]", ";-)", ";)", "*-)", "*)", ";-]", ";]", ";D", ";^)");
private $tongue = array(">:P", ":-P", ":P", "X-P", "x-p", ":-p", ":p", "=p", ":-Þ", ":Þ", ":-b", ":b", "=p", "=P");
private $surprise = array(">:o", ">:O", ":-O", ":O", "°o°", "°O°", ":O", "o_O", "o.O", "8-0");
private $annoyed = array(">:\\", ">:/", ":-/", ":-.", ":\\", "=/", "=\\", ":S");
private $cry = array(":'(", ";'(");

private $t_smile = "<img class=\"smiley\" src=\"/images/emoticons/smile.png\"/>";
private $t_laugh = "<img class=\"smiley\" src=\"/images/emoticons/laugh.png\"/>";
private $t_sad = "<img class=\"smiley\" src=\"/images/emoticons/sad.png\"/>";
private $t_wink = "<img class=\"smiley\" src=\"/images/emoticons/wink.png\"/>";
private $t_tongue = "<img class=\"smiley\" src=\"/images/emoticons/tongue.png\"/>";
private $t_surprise = "<img class=\"smiley\" src=\"/images/emoticons/surprise.png\"/>";
private $t_annoyed = "<img class=\"smiley\" src=\"/images/emoticons/annoyed.png\"/>";
private $t_cry = "<img class=\"smiley\" src=\"/images/emoticons/cry.png\"/>"

I am currently simply doing for example:

$str = str_replace($this->laugh, $this->t_laugh, $str);

for each group. It works fine but I need the replacement to occur only if the words are not surrounded by letters or other digits. In other words, I need to compile a regex which contain each emoticon array so that I can use preg_replace instead of str_replace. Is there a way I can do this easily instead of hardcoding the regex and escaping all the necessary characters?

EDIT:

Also, I need to match and replace the emoticons which appear in the beginning and end of a string, so a simple padding with a space technique won’t suffice.

EDIT 2:

I followed Mark’s example and pre-compiled the regex from the arrays using preg_quote as:

private $smile = "#(^|\W)(\>\:\]|\:-\)|\:\)|\:o\)|\:\]|\:3|\:c\)|\:\>|\=\]|8\)|\=\)|\:\}|\:\^\))($|\W)#";
private $laugh = "#(^|\W)(\>\:D|\:-D|\:D|8-D|x-D|X-D|\=-D|\=D|\=-3|8-\)|xD|XD|8D|\=3)($|\W)#";
private $sad = "#(^|\W)(\>\:\[|\:-\(|\:\(|\:-c|\:c|\:-\<|\:-\[|\:\[|\:\{|\>\.\>|\<\.\<|\>\.\<)($|\W)#";
private $wink = "#(^|\W)(\>;\]|;-\)|;\)|\*-\)|\*\)|;-\]|;\]|;D|;\^\))($|\W)#";
private $tongue = "#(^|\W)(\>\:P|\:-P|\:P|X-P|x-p|\:-p|\:p|\=p|\:-Þ|\:Þ|\:-b|\:b|\=p|\=P|xp|XP|xP|Xp)($|\W)#";
private $surprise = "#(^|\W)(\>\:o|\>\:O|\:-O|\:O|°o°|°O°|\:O|o_O|o\.O|8-0)($|\W)#";
private $annoyed = "#(^|\W)(\>\:\\|\>\:/|\:-/|\:-\.|\:\\|\=/|\=\\|\:S|\:\/)($|\W)#";
private $cry = "#(^|\W)(\:'\(|;'\()($|\W)#";

Works perfectly with preg_replace!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:52:46+00:00Added an answer on May 28, 2026 at 2:52 am

    If you want to use a regex:

    $pat = '#(^|\W)'.preg_quote($this->laugh,'#').'($|\W)#';
    $str = str_replace($pat, $this->t_laugh, $str);
    

    This basically means the emoticon can be at the start of the string or proceded by a non-word character, and must be followed by the end of the string or another non-word character. preg_quote is necessary in case your emoticon contains any special regex characters.

    Also, a better format might be:

    $emoticons = array(
        'smile' => array('<img src...', array('>:]',':-)',...),
        'laugh' => array('<img src....', array(...)),
        ...
    )
    

    Then you can loop over everything.


    Update

    Should use negative lookarounds instead to match side-by-side emoticons. Then it won’t try matching the surrounding spaces.

    <?php
    $smile = array(">:]", ":-)", ":)", ":o)", ":]", ":3", ":c)", ":>", "=]", "8)", "=)", ":}", ":^)");
    $laugh = array(">:D", ":-D", ":D", "8-D", "x-D", "X-D", "=-D", "=D", "=-3", "8-)");
    $sad = array(">:[", ":-(", ":(",  ":-c", ":c", ":-<", ":-[", ":[", ":{", ">.>", "<.<", ">.<");
    $wink = array(">;]", ";-)", ";)", "*-)", "*)", ";-]", ";]", ";D", ";^)");
    $tongue = array(">:P", ":-P", ":P", "X-P", "x-p", ":-p", ":p", "=p", ":-Ã", ":Ã", ":-b", ":b", "=p", "=P");
    $surprise = array(">:o", ">:O", ":-O", ":O", "°o°", "°O°", ":O", "o_O", "o.O", "8-0");
    $annoyed = array(">:\\", ">:/", ":-/", ":-.", ":\\", "=/", "=\\", ":S");
    $cry = array(":'(", ";'(");
    
    $ary = array_merge($smile, $laugh, $sad, $wink, $tongue,$surprise,$annoyed,$cry);
    
    foreach ($ary as $a)
    {
            $quoted[] = preg_quote($a, '#');
    }
    
    $regex = implode('|', $quoted);
    
    
    $full = '#(?!<\w)(' . $regex .')(?!\w)#';
    echo $full.PHP_EOL;
    $str = "Testing :) emoticons :D :(";
    
    preg_match_all($full, $str, $matches);
    print_r($matches[0]);
    

    Also, try to use single-quotes when writing regex patterns, because double-quotes allow escape sequences, and single quotes won’t interpret escape sequence. i.e., you sometimes need to double your slashes when using double quotes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to replace text bullet chars • with HTML ul & li tags within
I need to replace all iframe tags, stored as nvarchar in my database. I
I need to replace all of the image sources on my website from <img
I need replace spaces with &nbsp; inside HTML elements. Example: <table atrr=zxzx><tr> <td>adfa a
I need to replace the following: CREATE TABLE /*!32312 IF NOT EXISTS*/ `access` to
I need to quickly replace a listings website which has the following characteristics: smallish
I need replace slide effect to fade-out/fade-in effect on the prev and next slide
Need to replace a domain name on all the links on the page that
We need to replace the menu system in our main ASP.NET application. So naturally
I need to replace character (say) x with character (say) P in a string,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.