60°F

Aaron Parecki

  • Articles
  • Notes
  • Photos
  • Day 63: Autolinking Emoji to my Emoji List Pages #100DaysOfIndieWeb

    February 21, 2017

    Now that I have pages showing all posts that use a specific emoji such as šŸ‘, I found myself wanting to be able to click the emoji in my posts to see other posts with that emoji. This is the same as how my autolinked hashtags work, where you can click on any hashtag in a post and see other posts with that hashtag.

    This was relatively straightforward to accomplish with my new Emoji Detector library, there was only one trick needed. Because some emoji are composed of others (such as šŸ³ļøā€šŸŒˆ being composed of 🌈 plus šŸ³), the particular byte sequence for 🌈 actually appears twice in a string that has both of those emoji. Once for the standalone rainbow, and another as part of the rainbow flag. This means if we tried to replace all instances of 🌈 with a hyperlink, we'd actually catch the 🌈 byte in šŸ³ļøā€šŸŒˆ as well, breaking it into separate emoji!

    So to avoid this problem, I used two tricks.Ā 

    First, since the Emoji Detector library returns the number of code points in each emoji found, I first sort the list so that the longer emoji strings are first in the list.

    $emojis = \Emoji\detect_emoji($text); 
    usort($emojis, function($a,$b){ 
      return $a['num_points'] < $b['num_points']; 
    });
    

    Second, instead of doing an inline replace of the post text with the hyperlink, I create a mapping of placeholder strings for each emoji found, and replace the emoji with those placeholders instead.

    $map = [];
    foreach($emojis as $emoji) {
      if($emoji['short_name']) {
        $k = md5(mt_rand().$emoji['emoji']);
        $map[$k] = $emoji['emoji'];
        $text = str_replace($emoji['emoji'], $k, $text);
      }
    }
    

    Then, in a separate loop, I loop through the mapping and replace each random string with the emoji including a hyperlink wrapping it.

    foreach($map as $k=>$emoji) {
      $text = str_replace($k, '<a href="/emoji/'.$emoji.'" class="emoji">'.$emoji.'</a>', $text);
    }
    

    This way, I avoid the "clobbering" issue of double matching the duplicate byte sequences.

    Lastly, I noticed that when I use several emoji in a row, they end up appearing squished together like this:

    Since I was already wrapping each emoji with a hyperlink, I was then able to style them to add a bit of spacing around them, so now they appear a little nicer:

    Portland, Oregon
    Tue, Feb 21, 2017 3:29pm -08:00 #100daysofindieweb #emoji #p3k
    2 mentions

    Other Mentions

    • Aaron Parecki aaronparecki.com
      My 2017 Year in Review
      Thu, Jan 4, 2018 2:40pm -08:00
    • 100 Days of IndieWeb aaronparecki.com/tag/100daysofindieweb
      Day 63: Autolinking Emoji to my Emoji List Pages #100DaysOfIndieWeb: aaronparecki.com/2017/02/21/7/d…
      Tue, Feb 21, 2017 11:36pm +00:00 (via brid-gy.appspot.com)
Posted in /articles using quill.p3k.io

Hi, I'm Aaron Parecki, Director of Identity Standards at Okta, and co-founder of IndieWebCamp. I maintain oauth.net, write and consult about OAuth, and participate in the OAuth Working Group at the IETF. I also help people learn about video production and livestreaming. (detailed bio)

I've been tracking my location since 2008 and I wrote 100 songs in 100 days. I've spoken at conferences around the world about owning your data, OAuth, quantified self, and explained why R is a vowel. Read more.

  • Director of Identity Standards at Okta
  • IndieWebCamp Founder
  • OAuth WG Editor
  • OpenID Board Member

  • šŸŽ„ YouTube Tutorials and Reviews
  • šŸ  We're building a triplex!
  • ā­ļø Life Stack
  • āš™ļø Home Automation
  • All
  • Articles
  • Bookmarks
  • Notes
  • Photos
  • Replies
  • Reviews
  • Trips
  • Videos
  • Contact
© 1999-2025 by Aaron Parecki. Powered by p3k. This site supports Webmention.
Except where otherwise noted, text content on this site is licensed under a Creative Commons Attribution 3.0 License.
IndieWebCamp Microformats Webmention W3C HTML5 Creative Commons
WeChat ID
aaronpk_tv