简体   繁体   English

我无法在PHP上更改字符串编码

[英]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. 此代码将“ ASCII”作为输出。 Why? 为什么?

Your string has no UTF-8 specific characters, only ASCII. 您的字符串没有UTF-8特定字符,只有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 . 本演示中所示,您现在将获得UTF-8作为输出。

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. 但是,您无需运行转换就可以将UTF-8作为输出, mb_detect_encoding()发现该字符串为UTF-8而无需执行此步骤。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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