Latin-1/UTF-8编码php [英] Latin-1 / UTF-8 encoding php

查看:89
本文介绍了Latin-1/UTF-8编码php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UTF-8编码的数据库,其中混合了Latin-1. (我认为那是问题所在)

I have a db in UTF-8 encoding with a mixture of Latin-1. (I think that that is the problem)

这是字符在数据库中的外观.

This is how the characters look in the database.

Ä° (should be İ)
è

当我将标题设置为

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

然后字符显示为:

 İ
 �

当我删除标题时,它们按原样出现在数据库中.我希望他们这样出来:

When I remove the header, they come out as they are in the database. I want them to come out like this:

 İ
 è

在可能的情况下,我正在寻找一种在PHP中对此进行补救的方法.我目前无法校正数据本身,这是正确的做法.

I'm looking for a way to remedy this in PHP after the fact, if it is possible. I am unable to correct the data itself at this time, which would be the correct thing to do.

推荐答案

您的HTML输出需要采用单一编码,因此无法解决.这意味着需要先将不同编码的内容转换为HTML编码.尽管可以使用iconvmb_convert_encoding进行操作,但是您必须解决两个问题:

Your HTML output needs to be in a single encoding, there is no way around that. This means that content in different encodings needs to be converted to your HTML encoding first. While that is possible to do with iconv or mb_convert_encoding, there are two problems you have to solve:

  1. 您需要了解(或猜测)内容的当前编码
  2. 您需要在任何地方手动进行

例如,理论上的解决方案是选择UTF-8作为HTML编码,然后对要输出的所有字符串执行此操作:

For example, a theoretical solution would be to pick UTF-8 as your HTML encoding and then do this for all strings you are going to output:

$string = '...'; // from the database

// If it's not already UTF-8, convert to it
if (mb_detect_encoding($string, 'utf-8', true) === false) {
    $string = mb_convert_encoding($string, 'utf-8', 'iso-8859-1');
}

echo $string;

上面的代码假定非UTF-8内容是使用latin-1编码的,这根据您的问题是合理的.

The code above assumes that non-UTF-8 content is encoded in latin-1, which is reasonable according to your question.

这篇关于Latin-1/UTF-8编码php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆