如何转换“u00e9”转换为 utf8 字符,在 mysql 或 php 中?
我正在对一些正在导入 mysql 的混乱数据进行一些数据清理。
数据包含“伪”unicode 字符,这些字符实际上嵌入到字符串中,如“u00e9”等。
所以一个字段可能是..“Jalostotitlu00e1n” 我需要撕掉那个笨拙的“u00e1n”并将其替换为相应的utf字符
我可以在mysql中执行此操作,也许使用子字符串和CHR,但我通过PHP预处理数据,所以我也可以在那里执行此操作。
我已经知道如何配置 mysql 和 php 以使用 utf 数据。问题实际上出在我导入的源数据中。
谢谢
Im doing some data cleansing on some messy data which is being imported into mysql.
The data contains 'pseudo' unicode chars, which are actually embedded into the strings as 'u00e9' etc.
So one field might be.. 'Jalostotitlu00e1n'
I need to rip out that clumsy 'u00e1n' and replace it with the corresponding utf character
I can do this in either mysql, using substring and CHR maybe, but Im preprocssing the data via PHP, so I could do it there also.
I already know all about how to configure mysql and php to work with utf data. The problem is really just in the source data Im importing.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
/*
php 将 utf8 html 转换为 ansi 的函数
*/
/*
Function php for convert utf8 html to ansi
*/
有一个办法。将所有
uXXXX
替换为其 HTML 表示形式,并执行html_entity_decode()
即
echo html_entity_decode("Jalostotitlán");
每个 UTF
u1234
形式的字符可以在 HTML 中打印为ሴ
。但是进行替换非常困难,因为如果没有其他字符来标识 UTF 序列的开头,则可能会出现很多误报。一个简单的正则表达式可以是preg_replace('/u([\da-fA-F]{4})/', '&#x\1;', $str)
There's a way. Replace all
uXXXX
with their HTML representation and do anhtml_entity_decode()
I.e.
echo html_entity_decode("Jalostotitlán");
Every UTF character in the form
u1234
could be printed in HTML asሴ
. But doing a replace is quite hard, because there could be much false positives if there is no other char that identifies the beginning of an UTF sequence. A simple regex could bepreg_replace('/u([\da-fA-F]{4})/', '&#x\1;', $str)
我的 Twitter 时间线脚本将特殊字符(例如 é)返回到 \u00e9,因此我删除了反斜杠并使用 @rubbude 他的 preg_replace。
它对我有用,结果是:
De #Haarstichting 是中等内容
进入:
De #Haarstichting 是所有内容的中等内容
My twitter timeline script returns the special characters like é into \u00e9 so I stripped the backslash and used @rubbude his preg_replace.
It workes for me and it turns:
De #Haarstichting is h\u00e9t medium voor alles
Into:
De #Haarstichting is hét medium voor alles
虽然多年后才回复,但下次我需要它时,我会记得这个函数对我来说效果很好:
它与 utf8_decode 完全相同,但该函数已被弃用PHP 8.2.0
Although it's late to respond after so many years, for the next time I need it, I'll remember that this function worked nicely for me:
That's exactly the same as
utf8_decode
but that one is DEPRECATED as of PHP 8.2.0