Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mb#u substr()来自json#u decode()';d表情符号_Php_Encoding_Telegram - Fatal编程技术网

Php mb#u substr()来自json#u decode()';d表情符号

Php mb#u substr()来自json#u decode()';d表情符号,php,encoding,telegram,Php,Encoding,Telegram,我有以下JSON:“\ud83e\uddea test”,它被解码成,最终我明白了:给定字符串、偏移量和长度,其中 offset Integer Offset in UTF-16 code units to the start of the entity length Integer Length of the entity in UTF-16 code units offset以UTF-16代码单位表示的整数偏移量到实体开头的偏移量 长度以UTF-16代码单位表示的实体的整数

我有以下JSON:
“\ud83e\uddea test”
,它被解码成
,最终我明白了:给定字符串、偏移量和长度,其中

offset   Integer   Offset in UTF-16 code units to the start of the entity
length   Integer   Length of the entity in UTF-16 code units
offset以UTF-16代码单位表示的整数偏移量到实体开头的偏移量
长度以UTF-16代码单位表示的实体的整数长度

文档中说偏移量是以“到实体开头的UTF-16代码单位”给出的。如果我先使用
mb_convert\u encoding
将解码的JSON值转换为
UTF-16
,然后使用
mb_substr($data,2,null,'UTF-16')
,结果是
test
(作为一个8字节长的UTF-16字符串,因此您可能需要转换回UTF-8。)使用
2
对我来说似乎有点合乎逻辑,因为当他们说这是UTF-16代码单位的偏移量时,如果我正确解释的话,这意味着
3
指的是第三个字符,但由于PHP从0开始计数,所以必须减少1。最后一部分不是100%确定的,再举几个在不同位置/不止一个位置具有这种特殊字符的输入数据示例,可能有助于验证这一点。@CBroe不一定。来自实体的信息实际上是
3
。因为我们认为表情符号由2个字符组成,每个字符有2个字节,并且考虑到空间,我们在
“test”
之前有
3个
字符。PHP理解表情符号是一个4字节的字符(这对我来说更有意义)。基本上,我认为电报如何向我们发送信息(我认为这是不正确的)比PHP无法以这种方式处理的事实更重要。我要说的是,你对UTF-16的理解是错误的。我很确定内部字符编码(
mb\u internal\u encoding()
)是
UTF-8
。因此,
substr($decoded,5,4)
mb_substr($decoded,2,4)
iconv_substr($decoded,2,4)
应提取单词
test
。然而,
substr
是最差的,IMHO.@JosefZ实际上,任何编码都不起作用。见: