PHP Foreach 循环打印所有表情符号

PHP Foreach Loop to Print All Emojis

我看到 PHP 7 中对表情符号有更好的支持,但没有打包的 set/library 表情符号可供参考。截至目前,我必须在 https://apps.timwhitlock.info/emoji/tables/unicode 搜索并寻找我想要的表情符号的 UNICODE。

是否有一种更简单的方法来获取每个(最新的)表情符号,方法是循环遍历而不是引用我必须自己构建的数组(复制并粘贴每个 UNICODE)?

您可以定义范围并使用循环迭代并打印它们,而不是手动列出所有 unicode。

这可能是这样的:

$emojiUnicodeRange = [
[0x1f600, 0x1f64e],
[0x1f910, 0x1f91e],
[0x1f920, 0x1f927],
[0x1f300, 0x1f5ff],
[0x1f680, 0x1f6c1],
[0x1f950, 0x1f95e],
[0x1f980, 0x1f991]
];
foreach($emojiUnicodeRange as $range)
    for($emojiUnicode=$range[0];$emojiUnicode<=$range[1];$emojiUnicode++)
        echo html_entity_decode('&#'.$emojiUnicode.';', 0, 'UTF-8');

html_entity_decode('&#'.$emojiUnicode.';', 0, 'UTF-8')部分将十六进制数转换为实体并将其解码为utf-8。遗憾的是,据我所知,没有更简单的方法来实现这一目标。

您可以查看来自http://unicode.org/Public/emoji/6.0/emoji-data.txt but it seems like the main supported version is v5 which can be seen here http://unicode.org/Public/emoji/5.0/emoji-data.txt

的所有最新表情符号列表

您需要使用正则表达式解析结果以获取范围,然后您可以遍历 preg_match 的结果,如果范围有结束编号,您希望获取所有值十六进制范围之间。如果您必须经常执行此操作,您可能希望缓存这些结果。

$emojis = [];

$reg = "/^(?<start>[a-fA-F0-9]+)([\.]{2})?((?<end>[a-fA-F0-9]+))?\s+;/m";
$data = file_get_contents('http://unicode.org/Public/emoji/5.0/emoji-data.txt');

$matches = [];
preg_match_all($reg, $data, $matches);

$start = $matches['start'];
$end = $matches['end'];

for ($i = 0; $i < count($start); $i++) {
    $emojis[] = $start[$i];
    if (!empty($end[$i])) {
        for ($j = $start[$i] + 0x1; $j <= $end[$i]; $j += 0x1) {
            $emojis[] = is_int($j) ? dechex($j) : $j;
        }
    }
}

// $emojis contains valid emojis from the file

你会想要测试这个,我只是很快就把它放在一起了。

尝试以下操作:

<?php

$data = file_get_contents("https://apps.timwhitlock.info/emoji/tables/unicode");

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($data);
libxml_clear_errors();
$finder = new DomXPath($doc);
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' code ')]");
$unicodes = [];
$i = 1;
foreach ($nodes as $node) 
{
if($i % 2 === 0) {$i++;continue;}

    $unicode = trim($node->textContent);
    $unicodes[] = $unicode;
    file_put_contents("unicodes.txt", $unicode. "\r\n", FILE_APPEND);

    $i++;
}

var_dump($unicodes);

它将从站点获取所有 Unicodes 并将其存储在文件 unicodes.txt 和数组 $unicodes 中。这只是使用 DOMDocument 来废弃页面。然后你可以使用:

<?php

$emojis = file("unicodes.txt");

foreach($emojis as $emoji)
{
    $emoji = trim($emoji);
    $emoji = hexdec($emoji);
    echo "&#$emoji;";
}

您好,这是基于凯尔的更新答案

$emojis = [];

$reg = "/^(?<start>[A-F0-9 ]+)(?:[\.]{2})?((?<end>[A-F0-9 ]+))?.*;/m";
$data = file_get_contents('https://unicode.org/Public/emoji/14.0/emoji-sequences.txt');

$matches = [];
preg_match_all($reg, $data, $matches);

$start = str_replace(' ', '', $matches['start']);
$end = str_replace(' ', '', $matches['end']);

for ($i = 0; $i < count($start); $i++) {
    $emojis[] = $start[$i];
    if (!empty($end[$i])) {
        for ($j = hexdec($start[$i]) + 0x1; $j <= hexdec($end[$i]); $j += 0x1) {
            $emojis[] = dechex($j);
        }
    }
}