3

In PHP when I use the ord function in order to catch the ASCII code of my character I get this behavior:

ord("a") // return 97
chr(97) // return a

But when I use a special character like Œ the returns are different:

ord("Œ") // return 197
chr(197) // return �

All of my pages are encoded in utf8. This behaviour is the same for most of the special characters.

Has somebody seen this problem in the past? How can I fix it?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • 1
    `ord` only works on single-byte characters. If used on a multibyte character only the first byte will be returned, which is why you are getting weird results. – Sverri M. Olsen Sep 24 '15 at 20:51
  • 1
    possible duplicate of [How to get the character from unicode value in PHP?](http://stackoverflow.com/questions/1365583/how-to-get-the-character-from-unicode-value-in-php) – Don't Panic Sep 24 '15 at 20:52

2 Answers2

1

ord() and chr() both use the ASCII values of characters, which is a single byte encoding. Œ is not a valid character in ASCII.

You can get each byte of a multi-byte character by specifying the byte offset, as follows:

$oethel = "Œ";
$firstByte = ord($oethel[0]); // 197
$secondByte = ord($oethel[1]); // 146

Reversing the process, however, does not work, because assigning to a string byte offset converts that string to an array:

$newOethel = "";
$newOethel[0] = chr(197);
$newOethel[1] = chr(146);
echo $newOethel;
// Output is as follows: 
// PHP Notice:  Array to string conversion
// Array
Ghedipunk
  • 1,229
  • 10
  • 22
  • "assigning to a string byte offset converts that string to an array" — only for zero-length strings. If you had `$newOethel = "?";` then the output would be `Œ` ([demo](https://3v4l.org/9QrAu)). – salathe Oct 09 '15 at 19:02
0

The black diamond with a question mark is a display problem.

Review the details of black diamond in https://stackoverflow.com/a/38363567/1766831 . There are two cases; see which one fits.

Community
  • 1
  • 1
Rick James
  • 135,179
  • 13
  • 127
  • 222