将utf8字符转换为iso-88591并返回PHP [英] Convert utf8-characters to iso-88591 and back in PHP
问题描述
我的一些脚本使用不同的编码,当我尝试结合它们时,这是一个问题。
Some of my script are using different encoding, and when I try to combine them, this has becom an issue.
但是我不能改变它们的编码使用,而是我想从脚本A更改结果的编码,并将其用作脚本B中的参数。
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.
所以,是否有任何简单的方法来更改字符串从UTF-8到ISO-88591在PHP?我看过utf_encode和_decode,但是他们不会做我想要的。为什么不存在任何utf2iso() - 函数或类似?
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?
我不认为我有不能写入ISO-格式,所以不应该是一个巨大的问题。
I don't think I have characters that can't be written in ISO-format, so that shouldn't be an huge issue.
推荐答案
看看 iconv()
或 mb_convert_encoding()
。
顺便说一句:为什么不 utf8_encode()
和 utf8_decode()
为您工作?
Have a look at iconv()
or mb_convert_encoding()
.
Just by the way: why don't utf8_encode()
and utf8_decode()
work for you?
utf8_decode - 用
转换字符串ISO-8859用
编码的-1个字符UTF-8到单字节ISO-8859-1
utf8_decode — Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1
utf8_encode - 对ISO-8859进行编码-1
字符串到UTF-8
utf8_encode — Encodes an ISO-8859-1 string to UTF-8
所以基本上
$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');
都应该这样做 - 用 utf8_en / decode()
不需要特殊的扩展名, mb_convert_encoding()
需要ext / mbstring和 iconv()
需要ext / iconv 。
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字符转换为iso-88591并返回PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!