我无法在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.
答
My assumption is that because ASCII is a subset of UTF-8, that a purely ASCII "converted" to UTF-8 will be indistinguishable from ASCII.