php - 将 utf8 字符转换为 iso-88591 并返回 PHP

标签 php encoding utf-8 iso-8859-1

我的一些脚本使用了不同的编码,当我尝试组合它们时,这就成了一个问题。

但我不能改变他们使用的编码,相反我想改变脚本A结果的编码,并将其用作脚本B中的参数。

那么:有什么简单的方法可以在 PHP 中将字符串从 UTF-8 更改为 ISO-88591?我查看了 utf_encode 和 _decode,但它们不符合我的要求。为什么不存在任何“utf2iso()”函数或类似函数?

我不认为我有不能用 ISO 格式编写的字符,所以这应该不是一个大问题。

最佳答案

看看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 = 'ÄÖÜ'; // 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。

关于php - 将 utf8 字符转换为 iso-88591 并返回 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/374425/

相关文章:

php - 无法让此搜索表单按照我想要的方式工作

php - Symfony\组件\表单\异常\InvalidArgumentException : Could not load type "..."

Perl:从 cp1251 转换为 utf8

php - 如何让 PHP 通过 jQuery $.load() 实用函数将 utf-8 字符串传递给 JavaScript?

encoding - CDO.Message 编码问题

php - CakePHP 检查数据库中的特定值是否为 true

javascript - 单击 div 时在 jQuery 中发送 POST 请求

R,utf-8 字符似乎无法滑动

encryption - 加密脚本以嵌入文本文件

python - python中的编码问题