我能';不要在PHP上更改字符串编码

我能';不要在PHP上更改字符串编码,php,Php,此代码以“ASCII”作为输出。 为什么?您的字符串没有UTF-8特定字符,只有ASCII字符 添加一个: <?php $str = "Text"; $str = mb_convert_encoding($str, "UTF-8", mb_detect_encoding($str)); echo mb_detect_encoding($str); ?> 您现在将获得UTF-8作为输出,如中所示 但是,您不需要运行转换来获取UTF-8作为输出,mb\u detect

此代码以“ASCII”作为输出。
为什么?

您的字符串没有UTF-8特定字符,只有ASCII字符

添加一个:

<?php
   $str = "Text";
   $str = mb_convert_encoding($str, "UTF-8", mb_detect_encoding($str));
   echo mb_detect_encoding($str);
?>
您现在将获得
UTF-8
作为输出,如中所示


但是,您不需要运行转换来获取
UTF-8
作为输出,
mb\u detect\u encoding()
在没有此步骤的情况下检测字符串是
UTF-8

我的假设是,因为ASCII是UTF-8的子集,所以将纯ASCII“转换”为UTF-8的字符串与ASCII无法区分

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