我正在使用emojis,我有一个JSON文件,它将每个表情符号的冒号代码映射到它的Unicode字符。一段文件:
{":grinning:":"",
":grimacing:":"",
":grin:":"",
":joy:":"",
":smiley:":""}
.... it has more than 1200 elements
我用PHP获取此文件,对其进行解码,我在消息字符串中查找模式:***:
,因此我可以将此unicode单词替换为真正的表情符号。
事情是:我找到并获得模式,在表情符号数组中寻找它,它给了我"未定义索引"错误。
array_key()
为模式返回true。
这是我的PHP代码:
$message = $row['body'];
preg_match('/:(.*?):/', $row['body'], $matches, PREG_OFFSET_CAPTURE);
if (isset($matches[0])) {
//get Emoji map
$stringMap = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/v1/emoji-map.json");
$jsonMap = json_decode($stringMap, true);
foreach ($matches as $key => $value) {
$emojiColon = $value[0];
print key_exists($emojiColon, $jsonMap); // returns true
print ($emojiColon); // prints the founded pattern (ie. :smiley:)
$emoji = $jsonMap[$value[0]]; // this line returns the ERROR "Undefined Index"
print $emoji; // returns the EMOJI!!! WTF
$message = str_replace($emojiColon, $emoji, $message);
}
}
此代码段返回的错误是:
1:smiley:
<html>
<head><title>Slim Application Error</title>
</head>
<body><h1>Slim Application Error</h1>
<p>The application could not run because of the following error:</p>
<h2>Details</h2>
<div><strong>Code:</strong> 8</div>
<div><strong>Message:</strong> Undefined offset: 0</div>
<div><strong>File:</strong> /var/www/apid.inngage.com.br/v1/DBFunctions.php</div>
<div><strong>Line:</strong> 123</div>
<h2>Trace</h2>
<pre>#0 /var/www/apid.inngage.com.br/v1/DBFunctions.php(123): Slim::handleErrors(8, 'Undefined offse...', '/var/www/apid.i...', 123, Array)
如果设置了索引并且甚至返回了表情符号,为什么会返回此错误?