【发布时间】:2012-04-03 09:41:42
【问题描述】:
我有一个希腊文本,我想将其转换为不带空格的十六进制代码点。只是一大串文本。
这正是我要找的东西 -> Unicode Hexadecimal code points for PHP 但它没有提供他是如何做到的实际代码。
【问题讨论】:
标签: php
我有一个希腊文本,我想将其转换为不带空格的十六进制代码点。只是一大串文本。
这正是我要找的东西 -> Unicode Hexadecimal code points for PHP 但它没有提供他是如何做到的实际代码。
【问题讨论】:
标签: php
基于original code和这个问题的答案:How to get code point number for a given character in a utf-8 string?我把这个函数放在一起:
function utf8_to_unicode($str) {
$unicode = array();
$values = array();
$lookingFor = 1;
for ($i = 0; $i < strlen($str); $i++) {
$thisValue = ord($str[$i]);
if ($thisValue < 128)
$unicode[] = str_pad(dechex($thisValue), 4, "0", STR_PAD_LEFT);
else {
if (count($values) == 0) $lookingFor = ($thisValue < 224) ? 2 : 3;
$values[] = $thisValue;
if (count($values) == $lookingFor) {
$number = ($lookingFor == 3) ?
(($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64):
(($values[0] % 32) * 64) + ($values[1] % 64);
$number = strtoupper(dechex($number));
$unicode[] = str_pad($number, 4, "0", STR_PAD_LEFT);
$values = array();
$lookingFor = 1;
} // if
} // if
} // for
return ($unicode);
} // utf8_to_unicode
所以:
$greekString = "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ ";
$hexArray = utf8_to_unicode($greekString);
echo implode("", $hexArray);
将输出:
039103920393039403950396039703980399039A039B039C039D039E039F03A003A103A303A403A503A603A703A803A90032
【讨论】:
$unicode[] = str_pad(dechex($thisValue), 4, "0", STR_PAD_LEFT);
这对我有用:
header('Content-Type: text/html; charset=utf-8');
bin2hex(iconv('UTF-8', 'UTF-16BE', 'your message'));
【讨论】: