简体   繁体   中英

I can't change string encoding on PHP

<?php
   $str = "Text";
   $str = mb_convert_encoding($str, "UTF-8", mb_detect_encoding($str));
   echo mb_detect_encoding($str);
?>

This code is givin me "ASCII" as output. Why?

Your string has no UTF-8 specific characters, only ASCII.

Add one in:

$str = "Text È";
$str = mb_convert_encoding($str, "UTF-8", mb_detect_encoding($str));
echo mb_detect_encoding($str);

You'll get UTF-8 as output now, as seen in this demo .

However, you don't need to run the conversion to get UTF-8 as output, mb_detect_encoding() picks up that the string is UTF-8 without this step.

我的假设是,因为ASCII是UTF-8的子集,所以将纯ASCII“转换”为UTF-8不会与ASCII区分。

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