我有一个希腊文本,我想将其转换为没有空格的十六进制代码点.只是一大串文字.
这正是我正在寻找的 - > PHP的十六进制代码点,但它没有提供实际的代码,他是如何做到的.
基于原始代码和这个问题的答案:如何在utf-8字符串中获取给定字符的代码点编号?我把这个功能放在一起:
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
Run Code Online (Sandbox Code Playgroud)
所以:
$greekString = "???????????????????????? ";
$hexArray = utf8_to_unicode($greekString);
echo implode("", $hexArray);
Run Code Online (Sandbox Code Playgroud)
将输出:
039103920393039403950396039703980399039A039B039C039D039E039F03A003A103A303A403A503A603A703A803A90032
Run Code Online (Sandbox Code Playgroud)