简体   繁体   中英

PHP: chr Function Issue 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 can't the chr function provide me the € sign? How can i get the €? I really need this to work. Thank's in advance.

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

The Euro sign is a three byte character in UTF-8: 0xE2 0x82 0xAC , so ord("€") (with the UTF-8 symbol) 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 a multi-byte encoding like UTF-8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM