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

发布于 2024-07-10 02:17:46 字数 267 浏览 11 评论 0原文

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

但我无法更改他们使用的编码,而是我想更改脚本 A 结果的编码,并将其用作脚本 B 中的参数。

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

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

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.

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?

I don't think I have characters that can't be written in ISO-format, so that shouldn't be an huge issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

别闹i 2024-07-17 02:17:46

看看 iconv()mb_convert_encoding()
顺便说一句:为什么不 utf8_encode() 和 < a href="http://php.net/function.utf8-decode" rel="noreferrer">utf8_decode() 适合你吗?

utf8_decode — 转换字符串
ISO-8859-1 字符编码
UTF-8 到单字节 ISO-8859-1

utf8_encode — 编码 ISO-8859-1
字符串转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。

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 — 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

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.

手长情犹 2024-07-17 02:17:46

首先,不要使用不同的编码。 它会导致混乱,而 UTF-8 绝对是您应该在任何地方使用的编码。

您的输入很可能不是 ISO-8859-1,而是其他内容(ISO-8859-15、Windows-1252)。 要从这些转换,请使用 iconvmb_convert_encoding

尽管如此,utf8_encodeutf8_decode 应该适用于 ISO-8859-1。 如果您可以发布一个文件或 uuencoded 的链接,那就太好了或 base64 转换失败或产生意外结果的示例字符串。

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.

Chances are your input is not ISO-8859-1, but something else (ISO-8859-15, Windows-1252). To convert from those, use iconv or mb_convert_encoding.

Nevertheless, utf8_encode and utf8_decode should work for ISO-8859-1. It would be nice if you could post a link to a file or a uuencoded or base64 example string for which the conversion fails or yields unexpected results.

溺孤伤于心 2024-07-17 02:17:46

使用 html_entity_decode()htmlentities()

$html = html_entity_decode(htmlentities($html, ENT_QUOTES, 'UTF-8'), ENT_QUOTES , 'ISO-8859-1');

htmlentities() 将您的输入格式化为 UTF8html_entity_decode() 将其格式化回 ISO-8859-1

Use html_entity_decode() and htmlentities().

$html = html_entity_decode(htmlentities($html, ENT_QUOTES, 'UTF-8'), ENT_QUOTES , 'ISO-8859-1');

htmlentities() formats your input into UTF8 and html_entity_decode() formats it back to ISO-8859-1.

奢华的一滴泪 2024-07-17 02:17:46

最好使用

$value = mb_convert_encoding($value,'HTML-ENTITIES','UTF-8');

特别是当您使用 AJAX 调用提交“ISO-8859-1”时人物。 它适用于中文、日语、捷克语、德语和许多其他语言。


  1. 请参阅 https://www.php.net/manual/en /function.mb-convert-encoding.php
  2. HTML-ENTITIES 从 PHP 8.2 开始将被弃用,请参阅 https://www.php.net/manual/en/migration82.deprecated.php#migration82.deprecated.mbstring

It is much better to use

$value = mb_convert_encoding($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.


  1. See https://www.php.net/manual/en/function.mb-convert-encoding.php
  2. HTML-ENTITIES will be deprecated as of PHP 8.2, see https://www.php.net/manual/en/migration82.deprecated.php#migration82.deprecated.mbstring
梦初启 2024-07-17 02:17:46

在 head 中设置元标记,

 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 

使用链接 http://www.i18nqa.com/debug/ utf8-debug.html 替换你想要的符号字符。

然后使用 str_replace 就像

    $find = array('“', '’', '…', '—', '–', '‘', 'é', 'Â', '•', 'Ëœ', 'â€'); // en dash
                        $replace = array('“', '’', '…', '—', '–', '‘', 'é', '', '•', '˜', '”');
$content = str_replace($find, $replace, $content);

它是我使用的方法并且提供了很多帮助。 谢谢!

set meta tag in head as

 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 

use the link http://www.i18nqa.com/debug/utf8-debug.html to replace the symbols character you want.

then use str_replace like

    $find = array('“', '’', '…', '—', '–', '‘', 'é', 'Â', '•', 'Ëœ', 'â€'); // en dash
                        $replace = array('“', '’', '…', '—', '–', '‘', 'é', '', '•', '˜', '”');
$content = str_replace($find, $replace, $content);

Its the method i use and help alot. Thanks!

熟人话多 2024-07-17 02:17:46

您需要使用 iconv 包,特别是它的 iconv 函数。

You need to use the iconv package, specifically its iconv function.

时光磨忆 2024-07-17 02:17:46

我使用这个功能:

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

在我的博客中查看我的功能
http://www.unexpectedit.com/php/php-处理非英语字符-utf8

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

花海 2024-07-17 02:17:46

我用了:

function utf8_to_html ($data) {
    return preg_replace(
        array (
            '/ä/',
            '/ö/',
            '/ü/',
            '/é/',
            '/à/',
            '/è/'
        ),
        array (
            'ä',
            'ö',
            'ü',
            'é',
            'à',
            'è'
        ),
        $data 
    );
}

I used:

function utf8_to_html ($data) {
    return preg_replace(
        array (
            '/ä/',
            '/ö/',
            '/ü/',
            '/é/',
            '/à/',
            '/è/'
        ),
        array (
            'ä',
            'ö',
            'ü',
            'é',
            'à',
            'è'
        ),
        $data 
    );
}
余生共白头 2024-07-17 02:17:46

就我而言,上传名称包含这些字符的文件后,Filezilla 甚至看不到它们! 在 Cpanel 文件管理器中,它们显示为 ? (黑色背景下)。
这种组合使其在浏览器上正确显示(HTML 文档是西方编码的):

$dspFileName = utf8_decode(htmlspecialchars(iconv(mb_internal_encoding(), 'utf-8', basename($thisFile['path']))) );

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):

$dspFileName = utf8_decode(htmlspecialchars(iconv(mb_internal_encoding(), 'utf-8', basename($thisFile['path']))) );
清旖 2024-07-17 02:17:46
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');       
     }
}
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');       
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文