在PHP中从“Java Escape”转换为Index

时间:2013-02-28 18:25:57

标签: php unicode utf-8

是否有任何形式可以将Java Escape中的字符串转换为PHP中的索引unicode?

我有这个字符串:

$ str = "\ud83d\ude0e";

我需要在U +之后获得该部分:

U+1F60E 

或python代码:

u'\U0001f60e'

对应代码:http://www.charbase.com/1f60e-unicode-smiling-face-with-sunglasses

谢谢。

====编辑09/03 ====

对不起我的延迟并感谢您的回复,但我无法做到我需要的。

我需要用图像替换caracter,所以我这样做:

$src = "Hello "."\ud83d\ude0e";

$replaced = preg_replace("/\\\\u([0-9A-F]{1,8})/i", "&#x$1;", $src);

$replaced = str_replace('&#x1f60e', '<img src="data/emoji_new/1F60E.png">', $replaced);

$result = mb_convert_encoding($replaced, "UTF-8", "HTML-ENTITIES");

但是,不行。结果是:

"Hello ��"

还有什么想法??

再次感谢你!

1 个答案:

答案 0 :(得分:2)

PHP: Convert unicode codepoint to UTF-8非常相似

如果可以,直接从4字节字符开始。

$src = "Hello \u0001f60e";

$replaced = preg_replace("/\\\\u([0-9A-F]{1,8})/i", "&#x$1;", $src);

$result = mb_convert_encoding($replaced, "UTF-8", "HTML-ENTITIES");

echo "Result is [$result] and string length is ".mb_strlen($result);

在大多数人的浏览器中输出几乎肯定无法正常显示的内容。

Result is [Hello ] and string length is 10

或者来自两个UTF-16代码:

$src = "Hello "."\ud83d\ude0e";

$replaced = preg_replace("/\\\\u([0-9A-F]{1,4})/i", "&#x$1;", $src);

$result = mb_convert_encoding($replaced, "UTF-16", "HTML-ENTITIES");

$result = mb_convert_encoding($result, 'utf-8', 'utf-16');

echo "Result is [$result] and string length is ".mb_strlen($result)."\n";

$resultInHex = unpack('H*', $result);

$resultInHex = $resultInHex[1];

$resultSeparated = implode(', ', str_split($resultInHex, 2));

echo "in hex: ".$resultSeparated;

输出:

Result is [Hello ] and string length is 10
in hex: 48, 65, 6c, 6c, 6f, 20, f0, 9f, 98, 8e

对于想知道'什么是Java转义?'的每个人,Java都会在内部将所有字符编码为UTF-16。