1

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 'و'

chiliNUT
  • 18,989
  • 14
  • 66
  • 106
AndroidPlayer2
  • 346
  • 1
  • 2
  • 13

2 Answers2

2

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;
    }
}
chiliNUT
  • 18,989
  • 14
  • 66
  • 106
1

The following code works fine in all PHP 5, PHP 7

 $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
 return hexdec(bin2hex($ret));
AndroidPlayer2
  • 346
  • 1
  • 2
  • 13