Last active
July 18, 2018 13:15
-
-
Save rasteiner/013ac027a25fcf1ba6002e68e8a6e8ba to your computer and use it in GitHub Desktop.
create an emoji detecting regex
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
<?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