ord('Ö') is giving 195 and also ord('Ç') is giving 195 too. I didn't get what is the error. Can you guys help me?
Asked
Active
Viewed 324 times
0
-
2Take a look at [this](https://stackoverflow.com/questions/35575721/ord-doesnt-work-with-utf-8) thread. – Kamyar Mirzavaziri Mar 25 '20 at 12:12
-
Does this answer your question? [ord() doesn't work with utf-8](https://stackoverflow.com/questions/35575721/ord-doesnt-work-with-utf-8) – Umair Khan Mar 25 '20 at 12:52
-
Use `mb_ord('Ç','UTF-8');` instead of `ord();` – Mar 25 '20 at 13:15
1 Answers
1
ord — Convert the first byte of a string to a value between 0 and 255
https://www.php.net/manual/en/function.ord.php
The question is - what the charset of the source file? Since 'Ö' and 'Ç' both are not ASCII symbols, they are represented as two bytes in UTF-8 encoding
Ö - 0xC3 0x96
Ç - 0xC3 0x87
As you can see, both characters has first bytes 0xC3 (=195 dec.)
So, you need to decide what code you want to get?
For example, you can convert the UTF-8 string into Windows-1254:
print ord(iconv('UTF-8', 'Windows-1254', 'Ö')); // 214
print ord(iconv('UTF-8', 'Windows-1254', 'Ç')); // 199
Or you may want to get unicode Code point. To do that you can first convert the string into UTF-32, and then decode a 32-bit number:
function get_codepoint($utf8char) {
$bin = iconv('UTF-8', 'UTF-32BE', $utf8char); // convert to UTF-32 big endian
$a = unpack('Ncp', $bin); // unpack binary data
return $a['cp']; // get the code point
}
print get_codepoint('Ö'); // 214
print get_codepoint('Ç'); // 199
Or in php 7.2 and later you can simple use mb_ord
print mb_ord('Ö'); // 214
print mb_ord('Ç'); // 199

AterLux
- 4,566
- 2
- 10
- 13