将 utf8 字符转换为 iso-88591 并返回 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/374425/
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 utf8-characters to iso-88591 and back in PHP
提问by qualbeen
Some of my script are using different encoding, and when I try to combine them, this has becom an issue.
我的一些脚本使用不同的编码,当我尝试组合它们时,这已经成为一个问题。
But I can't change the encoding they use, instead I want to change the encodig of the result from script A, and use it as parameter in script B.
但是我无法更改他们使用的编码,而是想更改脚本 A 中结果的编码,并将其用作脚本 B 中的参数。
So: is there any simple way to change a string from UTF-8 to ISO-88591 in PHP? I have looked at utf_encode and _decode, but they doesn't do what i want. Why doesn't there exsist any "utf2iso()"-function, or similar?
那么:有没有什么简单的方法可以在 PHP 中将字符串从 UTF-8 更改为 ISO-88591?我看过 utf_encode 和 _decode,但它们没有做我想要的。为什么不存在任何“utf2iso()”功能或类似功能?
I don't think I have characters that can't be written in ISO-format, so that shouldn't be an huge issue.
我不认为我有不能以 ISO 格式编写的字符,所以这应该不是一个大问题。
回答by Stefan Gehrig
Have a look at iconv()or mb_convert_encoding().
Just by the way: why don't utf8_encode()and utf8_decode()work for you?
看看iconv()或mb_convert_encoding()。只是顺便:为什么不utf8_encode()和utf8_decode()为你工作?
utf8_decode— Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1
utf8_encode— Encodes an ISO-8859-1 string to UTF-8
utf8_decode— 将使用 UTF-8 编码的 ISO-8859-1 字符的字符串转换为单字节 ISO-8859-1
utf8_encode— 将 ISO-8859-1 字符串编码为 UTF-8
So essentially
所以本质上
$utf8 = '??ü'; // file must be UTF-8 encoded
$iso88591_1 = utf8_decode($utf8);
$iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $utf8);
$iso88591_2 = mb_convert_encoding($utf8, 'ISO-8859-1', 'UTF-8');
$iso88591 = '??ü'; // file must be ISO-8859-1 encoded
$utf8_1 = utf8_encode($iso88591);
$utf8_2 = iconv('ISO-8859-1', 'UTF-8', $iso88591);
$utf8_2 = mb_convert_encoding($iso88591, 'UTF-8', 'ISO-8859-1');
all should do the same - with utf8_en/decode()requiring no special extension, mb_convert_encoding()requiring ext/mbstring and iconv()requiring ext/iconv.
所有人都应该这样做 -utf8_en/decode()不需要特殊的扩展名,mb_convert_encoding()需要 ext/mbstring 和iconv()需要 ext/iconv。
回答by phihag
First of all, don't use different encodings. It leads to a mess, and UTF-8 is definitely the one you should be using everywhere.
首先,不要使用不同的编码。它会导致一团糟,而 UTF-8 绝对是你应该在任何地方使用的。
Chances are your input is not ISO-8859-1, but something else (ISO-8859-15, Windows-1252). To convert from those, use iconvor mb_convert_encoding.
您的输入可能不是 ISO-8859-1,而是其他内容(ISO-8859-15、Windows-1252)。要从这些转换,请使用iconv或mb_convert_encoding.
Nevertheless, utf8_encodeand utf8_decodeshould work for ISO-8859-1. It would be nice if you could post a link to a file or a uuencodedor base64example string for which the conversion fails or yields unexpected results.
尽管如此,utf8_encode并且utf8_decode应该适用于 ISO-8859-1。如果您可以发布指向转换失败或产生意外结果的文件或uuencoded或base64示例字符串的链接,那就太好了。
回答by user2842936
set meta tag in head as
将 head 中的元标记设置为
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
use the link http://www.i18nqa.com/debug/utf8-debug.htmlto replace the symbols character you want.
使用链接http://www.i18nqa.com/debug/utf8-debug.html替换您想要的符号字符。
then use str_replace like
然后使用 str_replace 之类的
$find = array('a?', 'a?', 'a|', 'a”', 'a“', 'a?', '??', '?', 'a¢', '??', 'a'); // en dash
$replace = array('“', ''', '…', '—', '–', '‘', 'é', '', '?', '?', '”');
$content = str_replace($find, $replace, $content);
Its the method i use and help alot. Thanks!
它的方法我使用和帮助很多。谢谢!
回答by VINAY KANT
It is much better to use
使用它要好得多
$value = mb_convert_encode($value,'HTML-ENTITIES','UTF-8');
$value = mb_convert_encode($value,'HTML-ENTITIES','UTF-8');
Specially when you are using AJAX call for submitting 'ISO-8859-1' characters. It works for Chinese, Japanese, Czech, German and many more languages.
特别是当您使用 AJAX 调用提交“ISO-8859-1”字符时。它适用于中文、日语、捷克语、德语和更多语言。
回答by Fernando CR
Use html_entity_decode()and htmlentities().
使用html_entity_decode()和htmlentities()。
$html = html_entity_decode(htmlentities($html, ENT_QUOTES, 'UTF-8'), ENT_QUOTES , 'ISO-8859-1');
htmlentities()formats your input into UTF8and html_entity_decode()formats it back to ISO-8859-1.
htmlentities()格式化您的投入UTF8和html_entity_decode()格式回ISO-8859-1。
回答by user109764
In my case after files with names containing those characters were uploaded, they were not even visible with Filezilla! In Cpanel filemanager they were shown with ? (under black background). And this combination made it shown correctly on the browser (HTML document is Western-encoded):
就我而言,在上传名称包含这些字符的文件后,Filezilla 甚至看不到它们!在 Cpanel 文件管理器中,它们显示为 ? (在黑色背景下)。这种组合使它在浏览器上正确显示(HTML 文档是西方编码的):
$dspFileName = utf8_decode(htmlspecialchars(iconv(mb_internal_encoding(), 'utf-8', basename($thisFile['path']))) );
回答by Martin v. L?wis
回答by Martin v. L?wis
I used:
我用了:
function utf8_to_html ($data) {
return preg_replace(
array (
'/?/',
'/?/',
'/ü/',
'/é/',
'/à/',
'/è/'
),
array (
'ä',
'ö',
'ü',
'é',
'à',
'è'
),
$data
);
}
回答by Ignacio Pascual
I use this function:
我使用这个功能:
function formatcell($data, $num, $fill=" ") {
$data = trim($data);
$data=str_replace(chr(13),' ',$data);
$data=str_replace(chr(10),' ',$data);
// translate UTF8 to English characters
$data = iconv('UTF-8', 'ASCII//TRANSLIT', $data);
$data = preg_replace("/[\'\"\^\~\`]/i", '', $data);
// fill it up with spaces
for ($i = strlen($data); $i < $num; $i++) {
$data .= $fill;
}
// limit string to num characters
$data = substr($data, 0, $num);
return $data;
}
echo formatcell("YES UTF8 String Zürich", 25, 'x'); //YES UTF8 String Z??richxxx
echo formatcell("NON UTF8 String Zurich", 25, 'x'); //NON UTF8 String Zurichxxx
Check out my function in my blog http://www.unexpectedit.com/php/php-handling-non-english-characters-utf8
在我的博客http://www.unexpectedit.com/php/php-handling-non-english-characters-utf8 中查看我的功能
回答by user1786647
function parseUtf8ToIso88591(&$string){
if(!is_null($string)){
$iso88591_1 = utf8_decode($string);
$iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $string);
$string = mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8');
}
}

