php 将 ASCII 转换为 UTF-8 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4983989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert ASCII TO UTF-8 Encoding
提问by user614856
How to convert ASCII encoding to UTF8 in PHP
如何在 PHP 中将 ASCII 编码转换为 UTF8
回答by Quentin
ASCII is a subset of UTF-8, so if a document is ASCII then it is already UTF-8.
ASCII 是 UTF-8 的子集,因此如果文档是 ASCII,那么它已经是 UTF-8。
回答by Dmitri
If you know for sure that your current encoding is pure ASCII, then you don't have to do anything because ASCII is already a valid UTF-8.
如果您确定您当前的编码是纯 ASCII,那么您无需执行任何操作,因为 ASCII 已经是有效的 UTF-8。
But if you still want to convert, just to be sure that its UTF-8, then you can use iconv
但是如果你仍然想转换,只是为了确定它的UTF-8,那么你可以使用iconv
$string = iconv('ASCII', 'UTF-8//IGNORE', $string);
The IGNORE will discard any invalid characters just in case some were not valid ASCII.
IGNORE 将丢弃任何无效字符,以防万一一些不是有效的 ASCII。
回答by albertoiNET
回答by Radek M
"ASCII is a subset of UTF-8, so..." - so UTF-8 is a set? :)
“ASCII 是 UTF-8 的子集,所以......” - 所以 UTF-8 是一个集合?:)
In other words: any string build with code points
from x00 to x7F has indistinguishable representations(byte sequences) in ASCII and UTF-8. Converting such string is pointless.
换句话说:code points
从 x00 到 x7F构建的任何字符串在 ASCII 和 UTF-8 中都具有无法区分的表示(字节序列)。转换这样的字符串是没有意义的。
回答by thomas
Use utf8_encode()
用 utf8_encode()
Man page can be found here http://php.net/manual/en/function.utf8-encode.php
手册页可以在这里找到http://php.net/manual/en/function.utf8-encode.php
Also read this article from Joel on Software. It provides an excellent explanation if what Unicode is and how it works. http://www.joelonsoftware.com/articles/Unicode.html
另请阅读 Joel 关于软件的这篇文章。它提供了一个很好的解释 Unicode 是什么以及它是如何工作的。http://www.joelonsoftware.com/articles/Unicode.html
回答by Marcin ?urek
Using iconv looks like best solution but i my case I have Notice form this function: "Detected an illegal character in input string in" (without igonore). I use 2 functions to manipulate ASCII strings convert it to array of ASCII code and then serialize:
使用 iconv 看起来是最好的解决方案,但我的情况是我有这个函数的通知:“在输入字符串中检测到一个非法字符”(没有 igonore)。我使用 2 个函数来操作 ASCII 字符串,将其转换为 ASCII 代码数组,然后序列化:
public static function ToAscii($string) {
$strlen = strlen($string);
$charCode = array();
for ($i = 0; $i < $strlen; $i++) {
$charCode[] = ord(substr($string, $i, 1));
}
$result = json_encode($charCode);
return $result;
}
public static function fromAscii($string) {
$charCode = json_decode($string);
$result = '';
foreach ($charCode as $code) {
$result .= chr($code);
};
return $result;
}