In PHP,
How can I convert the Unicode value out of mb_convert_encoding into integer number
$A = mb_convert_encoding('و',"UTF-8");
$A = 'و'
;
I need to convert 'و'
to 1608
which is the decimal equivalent of 'و'
In PHP,
How can I convert the Unicode value out of mb_convert_encoding into integer number
$A = mb_convert_encoding('و',"UTF-8");
$A = 'و'
;
I need to convert 'و'
to 1608
which is the decimal equivalent of 'و'
You want mb_ord()
. mb_ord()
gives you the code point of a unicode character and mb_chr()
is the inverse.
<?php
$A = 'و';
echo mb_ord($A); // 1608
echo mb_chr(1608); //و
There's a php5 polyfill on the man page if you can't use php7. I modified it slightly from the one on the man page.
if (!function_exists('mb_ord')) {
function mb_ord($u) {
$k = mb_convert_encoding($u,"UTF-8");
$k1 = ord(substr($k, 0, 1));
$k2 = ord(substr($k, 1, 1));
return $k2 * 256 + $k1;
}
}
The following code works fine in all PHP 5, PHP 7
$ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
return hexdec(bin2hex($ret));