Last active
June 4, 2017 18:42
-
-
Save icetee/b7f69ed4814280ebf263760c6873eb25 to your computer and use it in GitHub Desktop.
Change unicode Emojis to UTF-32 code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="emojiReplaceDemo"></div> | |
<script type="text/javascript"> | |
(function($) { | |
$(document).ready(function() { | |
var content = searchEmojis("🙊", function(match) { | |
return '<img class="emojioneemoji" src="https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/png/'+ uni2utf32(match) +'.png" alt="'+ match +'" />'; | |
}); | |
$('#emojiReplaceDemo').html(content); | |
}); | |
})(jQuery); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ReplaceEmojis @icetee - https://gist.github.com/icetee/b7f69ed4814280ebf263760c6873eb25 | MIT */ | |
function uni2utf32(str) { | |
var utf16 = [], | |
utf32 = ""; | |
for (var i = 0; i < str.length; i++) { | |
utf16.push( ('0x' + str.charCodeAt(i).toString(16).toUpperCase()) - 0 ); | |
} | |
utf32 = (utf16[0] - 0xD800) * 0x400 + utf16[1] - 0xDC00 + 0x10000; | |
return utf32.toString(16); | |
} | |
function searchEmojis(str, cb) { | |
if (!str) str = ''; | |
var pattern = /([\uD800-\uDBFF][\uDC00-\uDFFF])/g, | |
is_matched = str.match(pattern); | |
if (typeof cb !== 'function' || !is_matched) return str; | |
return str.replace(pattern, function (match) { | |
return cb(match); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ReplaceEmojis @icetee - https://gist.github.com/icetee/b7f69ed4814280ebf263760c6873eb25 | MIT */ | |
function uni2utf32(d){var a=[],c="";for(var b=0;b<d.length;b++){a.push(("0x"+d.charCodeAt(b).toString(16).toUpperCase())-0)}c=(a[0]-55296)*1024+a[1]-56320+65536;return c.toString(16)}function searchEmojis(d,a){if(!d){d=""}var c=/([\uD800-\uDBFF][\uDC00-\uDFFF])/g,b=d.match(c);if(typeof a!=="function"||!b){return d}return d.replace(c,function(e){return a(e)})}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment