如何将带有“(ISO-8859-1)字符的字符串转换为普通(UTF-8)字符? [英] How to convert String with “ (ISO-8859-1) characters to normal (UTF-8)characters?

查看:40
本文介绍了如何将带有“(ISO-8859-1)字符的字符串转换为普通(UTF-8)字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>

我在数据库中有很多原始 html 字符串.所有的文字都有这些奇怪的字符.我如何转换为普通文本以将其保存回数据库.

i have lot a raw html string in database. all the text have these weird characters. how can i convert to normal text for saving back it back in database.

$final = '<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>';
$final = utf8_encode($final);

$final = htmlspecialchars_decode($final);

$final = html_entity_decode($final, ENT_QUOTES, "UTF-8");

$final = utf8_decode($final);

echo $final;

我尝试了上面的代码,它在网络浏览器中正确显示,但仍然在数据库中保存了相同的奇怪字符.

i tried above code, it displays correctly in web browser but still saving the same weird characters in database.

数据库的字符集是utf-8

the charset of database is utf-8

推荐答案

$final = '<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>';

$final = str_replace("Â", "", $final);
$final = str_replace("’", "'", $final);
$final = str_replace("“", '"', $final);
$final = str_replace('â€"', '-', $final);
$final = str_replace('â€', '"', $final);

对于过去的数据,我用 UTF-8 字符替换了奇怪的字符.

for past datas, i replaced the weird characters with UTF-8 characters.

为了将来的数据,我在 php、html 和数据库连接中将字符集设置为 utf8.

for future datas, i made the charset to utf8 in php, html and databases connections.

这篇关于如何将带有“(ISO-8859-1)字符的字符串转换为普通(UTF-8)字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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