gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:53:08 30 4
gpt4 key购买 nike

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

但我不能改变他们使用的编码,相反我想改变脚本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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com