Skip to content

Instantly share code, notes, and snippets.

@rasteiner
Last active July 18, 2018 13:15
Show Gist options
  • Save rasteiner/013ac027a25fcf1ba6002e68e8a6e8ba to your computer and use it in GitHub Desktop.
Save rasteiner/013ac027a25fcf1ba6002e68e8a6e8ba to your computer and use it in GitHub Desktop.
create an emoji detecting regex
<?php
$data = explode("\n", file_get_contents('https://unicode.org/Public/emoji/11.0/emoji-data.txt'));
$ranges = [];
$toint = function($str) {
return intval($str, 16);
};
foreach ($data as $line) {
$line = trim(preg_replace('/[#;].*/', '', $line));
if($line) {
$ranges[] = strpos($line, '..') !== false ? array_map($toint, explode('..', $line)) : $toint($line);
}
}
$chr = function($i) {
return iconv('UTF-32', 'UTF-8', pack('V', $i));
};
echo '/[' . implode(array_map(function($range) use ($chr) {
if(is_array($range)) {
return $chr($range[0]) . '-' . $chr($range[1]);
}
return $chr($range);
}, $ranges)) . ']/u';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment