gpt4 book ai didi

php - 如何将带有“(ISO-8859-1)字符的字符串转换为普通(UTF-8)字符?

转载 作者:太空宇宙 更新时间:2023-11-03 10:36:11 25 4
gpt4 key购买 nike

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

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

$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;

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

数据库的字符集是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 字符替换了奇怪的字符。

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

关于php - 如何将带有“(ISO-8859-1)字符的字符串转换为普通(UTF-8)字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48045252/

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