phpord

php function ord problem ord ("ü") return


I'm passing some functions from VB to PHP I have a problem using the php - ord method.

example :

ord ("a") = 97 OK
ord ("ü") = 195 NOT OK

The result of ord ("ü") should be 50108

in VB ASC ("ü") = 50108 is for a function to encrypt a password.


Solution

  • As mentioned in the documentation the php function ord() cannot handle unicode characters. Beside that the documentation says:

    ord — Convert the first byte of a string to a value between 0 and 255

    You can use the binary safe function mb_ord() instead.

    <?php
    var_dump(mb_ord('ü', 'ascii')); // => 195
    var_dump(mb_ord('ü', 'utf-16') // => 50108
    

    As you can see here the decimal value in bytes is 195 and the strict decimal value is 50108. It all depends on the encoding of the char.