Search code examples
phpcharacter-encodingcyrillicord

PHP function ord() returns wrong code of cirilyc charecter


The utf-8 charcode of Russian 'A' is 1040 (decimal). Javascript do it right:

> 'А'.charCodeAt(0)
> 1040

But PHP code

<?php echo ord('А');?>

returns 208.

Please note that in the beginning of the PHP code I have:

mb_internal_encoding( 'UTF-8' );
setlocale( LC_CTYPE, 'ru_RU' );

How can I implement coding and decoding of UTF-8 characters in PHP? Use another function instead of ord?


Solution

  • <?php
        mb_internal_encoding('UTF-8');
        header('Content-Type: text/html; charset=UTF-8');
    ?>
    <html>
        <head>
            <meta charset="UTF-8" />
        </head>
        <body>
        <?php
            $the_char   = 'А';
            $byte_1     = $the_char[0];
            $byte_2     = $the_char[1];
            print (ord($byte_1) - 192) * 64 + (ord($byte_2) - 128);
        ?>
        </body>
    </html>