编码mysql时出错 - >如何重新转换为其他的东西? [英] Error in encoding mysql -> How can I reconvert it to something else?
问题描述
我在一段时间之前开始使用一个网站,在我的数据库和网站中使用错误的CHARSET。 HTML被设置为ISO ...,数据库为拉丁语...,页面被保存在西方拉丁语...一个大乱。
在法语中,所以我创建了一个函数,将é替换为& eacute;
。暂时解决了这个问题。
我刚刚学习了更多关于编程,现在我的文件保存为Unicode UTF-8,HTML是UTF-8,我的MySQL表列设置为ut8_encoding ...
我试图将音符移回é而不是& eacute ;
,但在MySQL和显示页面时,我得到了(?)或奇怪的字符â的通常的字符集问题。
我需要找到一种方法来更新我的sql,通过一个函数来清理字符串,以便它最终可以恢复正常。目前我的功能看起来像这样但不工作:
函数stripAcc3($ value){
$ ent = array(
'& agrave;'=>'à',
'& acirc;'=>'â',
'& ugrave ;'=>'ù',
'& ucirc;'=>'û',
'& eacute;'=>'é',
'& egrave;'=>'è',
'& ecirc;'=>'ê',
'& ccedil;'=>'ç',
'& ; Ccedil;'=>'Ç',
& icirc;=>'î',
& Iuml;= & ouml;=>'ö',
& ocirc;=>'ô',
& euml;=>'ë',
& uuml;=>'ü',
& Auml;=>'ä',
& euro;=>'€',
& prime;=>',
é=>é
);
return strtr($ value,$ ent);
}
欢迎任何帮助。提前致谢。如果您需要代码,请告诉我哪个部分。
UPDATE
你想要的赏金点,我需要详细的说明如何做。感谢。
尝试使用以下函数,它应该处理您描述的所有问题:
function makeStringUTF8($ data)
{
if(is_string($ data)=== true)
{
//有html实体?
if(strpos($ data,'&')!== false)
{
//如果是这样,请恢复正常
$ data = html_entity_decode ,ENT_QUOTES,'UTF-8');
}
//确保它是UTF-8
if(function_exists('iconv')=== true)
{
return @iconv ('UTF-8','UTF-8 // IGNORE',$ data);
}
else if(function_exists('mb_convert_encoding')=== true)
{
return mb_convert_encoding($ data,'UTF-8','UTF -8');
}
return utf8_encode(utf8_decode($ data));
}
else if(is_array($ data)=== true)
{
$ result = array();
foreach($ data as $ key => $ value)
{
$ result [makeStringUTF8($ key)] = makeStringUTF8($ value);
}
return $ result;
}
return $ data;
}
关于如何使用这个的具体说明,我建议如下: / p>
- 将旧的拉丁数据库(我希望您仍然拥有它)的内容导出为SQL / CSV转储 * / li>
- 对文件内容使用上述函数并将结果保存到另一个文件
- 将上一步中生成的文件导入UTF-8 示例:
pre>file_put_contents('utf8.sql',makeStringUTF8(file_get_contents('latin.sql')));
如果不让我知道,应该这样做。
I started a website some time ago using the wrong CHARSET in my DB and site. The HTML was set to ISO... and the DB to Latin... , the page was saved in Western latin... a big mess.
The site is in French, so I created a function that replaced all accents like "é" to "
é
". Which solved the issue temporarily.I just learned a lot more about programming, and now my files are saved as Unicode UTF-8, the HTML is in UTF-8 and my MySQL table columns are set to ut8_encoding...
I tried to move back the accents to "é" instead of the "
é
", but I get the usual charset issues with the (?) or weird characters "â" both in MySQL and when the page is displayed.I need to find a way to update my sql, through a function that cleans the strings so that it can finally go back to normal. At the moment my function looks like this but doesn't work:
function stripAcc3($value){ $ent = array( 'à'=>'à', 'â'=>'â', 'ù'=>'ù', 'û'=>'û', 'é'=>'é', 'è'=>'è', 'ê'=>'ê', 'ç'=>'ç', 'Ç'=>'Ç', "î"=>'î', "Ï"=>'ï', "ö"=>'ö', "ô"=>'ô', "ë"=>'ë', "ü"=>'ü', "Ä"=>'ä', "€"=>'€', "′"=> "'", "é"=> "é" ); return strtr($value, $ent); }
Any help welcome. Thanks in advance. If you need code, please tell me which part.
UPDATE
If you want the bounty points, I need detailed instructions on how to do it. Thanks.
解决方案Try using the following function instead, it should handle all the issues you described:
function makeStringUTF8($data) { if (is_string($data) === true) { // has html entities? if (strpos($data, '&') !== false) { // if so, revert back to normal $data = html_entity_decode($data, ENT_QUOTES, 'UTF-8'); } // make sure it's UTF-8 if (function_exists('iconv') === true) { return @iconv('UTF-8', 'UTF-8//IGNORE', $data); } else if (function_exists('mb_convert_encoding') === true) { return mb_convert_encoding($data, 'UTF-8', 'UTF-8'); } return utf8_encode(utf8_decode($data)); } else if (is_array($data) === true) { $result = array(); foreach ($data as $key => $value) { $result[makeStringUTF8($key)] = makeStringUTF8($value); } return $result; } return $data; }
Regarding the specific instructions of how to use this, I suggest the following:
- export your old latin database (I hope you still have it) contents as an SQL/CSV dump *
- use the above function on the file contents and save the result on another file
- import the file you generated in the previous step into the UTF-8 aware schema / database
* Example:
file_put_contents('utf8.sql', makeStringUTF8(file_get_contents('latin.sql')));
This should do it, if it doesn't let me know.
这篇关于编码mysql时出错 - >如何重新转换为其他的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!