"; echo ord("€") . "
"; echo chr(128) . ...">

PHP: chr Function Problem with Special Characters

In PHP, I have the following code:

<?php echo "€<br>"; echo ord("€") . "<br>"; echo chr(128) . "<br>"; 

And I get the following output:

 € 128   

Why doesn't the chr function give me the € sign? How can i get €? I really need this to work. Thanks in advance.

+5
source share
1 answer

chr and ord only work with single-byte ASCII characters. More specifically, ord looks only at the first byte of its parameter.

The Euro sign is a three-byte character in UTF-8: 0xE2 0x82 0xAC , therefore ord("€") (with the UTF-8 character) returns 226 (0xE2)

For characters that are also present in the ISO-8859-1 (latin1) character set, you can use utf8_encode() and utf8_decode() , but unfortunately the € sign is not contained there, so utf8_decode('€') will return "? " and cannot be converted back.

TL DR: You cannot use ord and chr with multibyte encoding like UTF-8

+4
source

Source: https://habr.com/ru/post/1232617/


All Articles