Exemple #1
0
 /**
  * @param string string
  *
  * @return string
  */
 public function replaceNamedWithUnicode($string)
 {
     $index = $this->getIndex();
     return preg_replace_callback($index->getEmojiNameRegex(), function ($matches) use($index) {
         $emoji = $index->findByName($matches[1]);
         return UnicodeUtil::convertUnicodeToString($emoji['unicode']);
     }, $string);
 }
Exemple #2
0
$emojiUnicodes = [];
$emojiUnicodeRegexParts = [];
foreach ($emojis as $index => $emoji) {
    if (isset($emoji['name'])) {
        // Create a map of emoji names to the hash index
        $emojiNames[$emoji['name']] = $index;
    }
    if (isset($emoji['aliases'])) {
        foreach ($emoji['aliases'] as $alias) {
            // Create a map of emoji names to the hash index
            $emojiNames[$alias] = $index;
        }
    }
    if (isset($emoji['unicode'])) {
        $string = '';
        foreach (explode('-', $emoji['unicode']) as $unicode) {
            // Get string from unicode parts
            $string .= UnicodeUtil::convertUnicodeToString($unicode);
        }
        // Create a map of unicode emoji characters to the hash index
        $emojiUnicodes[$string] = $index;
        $emojiUnicodeRegexParts[] = UnicodeUtil::formatRegexString($string);
    }
}
// Build the unicode regex
$emojiUnicodeRegex = sprintf('/%s/', implode('|', $emojiUnicodeRegexParts));
// Build the name regex
$emojiNameRegex = sprintf('/:(%s):/', implode('|', array_map(function ($name) {
    return preg_quote($name, '/');
}, array_keys($emojiNames))));
echo str_replace(['{{emojis}}', '{{emojiNames}}', '{{emojiUnicodes}}', '{{emojiNameRegex}}', '{{emojiUnicodeRegex}}'], [var_export($emojis, true), var_export($emojiNames, true), var_export($emojiUnicodes, true), var_export($emojiNameRegex, true), var_export($emojiUnicodeRegex, true)], $template);