Jump to content

Using regex to deal with emoji icons


speedy33417

Recommended Posts

I have a website that's visited by iPhone and iPhone Touch mostly. The site takes comments and has a self-built forum. The iPhone has its own emoticons called emoji. When used in text they look like this: 

 

Comments and posts are stored with the real HTML code for the icon, such as above. What I'm trying to find a solution for is replacing them with image tags when displayed.

 

For example I have user comment in my database like so.

 

$message = "This website is cool!& #57612; Keep up the good work!";
// Here comes the regex magic...
$message = "This website is cool!<img src=\"http://www.mysite.com/images/emoji123.png\" /> Keep up the good work!";

 

The problem is that there are over 500 icons. I was thinking to use two arrays to store both formats:

 

$emoji[123] = "& #57612;";
$html[123] = "<img src=\"http://www.mysite.com/images/emoji123.png\" />";

 

 

So if I find a emoji script in the text then I check which one it is from my array and then replace it with the corresponging html versio in the text.

 

The problem is that I suck at Regex, and would really welcome any help that you can throw at me.

 

Thanks!

Link to comment
Share on other sites

Many possible solutions, here's one off the top of my head...

 

function regify( $input ) {
   return '#\b'.preg_quote( $input, '#' ) . '\b#';
}

function imagify( $input ) {
   return '<img src="http://www.mysite.com/images/' . $input . '" />';
}

$tags = array ('& #57612', '& #somethingelse');
$files = array('emoji123.png', 'emoji456.png');

$patterns = array_map( $tags , 'regify' );
$replacements = array_map( $files, 'imagify' );

$output = preg_replace( $patterns, $replacements, $input );

Disclaimer: I just got up, this may not work as is :).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.