mysql php 编码
我有一个 UTF8 编码的 mysql 数据库。但是,由于我之前继承的一些转换问题,某些字符串已错误地保存到数据库中。
例如,£ 应保存为 £,但在许多地方它被保存为类似 ‚€ 的内容。
我已经能够找到表中所有编码错误的记录。从该 varchar 数据库字段中删除所有不必要的字符的最简单方法是什么?
我在 php 中尝试过 preg_replace,但这似乎实际上没有做任何事情。
return preg_replace("[^A-Za-z0-9£]", "", $string);
I have a mysql database that is UTF8 encoded. However, due to some previous conversion issues that I inherited, certain strings have been saved incorrectly to the database.
For example, £ should be saved as £, but in many places its been saved as something like £.
I have been able to track down all the records in the table that have been incorrectly encoded. Whats the easiest way for me to remove all the unncessary characters from this varchar database field?
I've tried preg_replace in php, but this doesnt seem to actually do anything.
return preg_replace("[^A-Za-z0-9£]", "", $string);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将正则表达式括在分隔符中:
You must enclose the regex in delimiters:
preg_replace 调用返回什么,是不受影响的字符串还是 NULL?如果为空,则根据 preg_replace 手册 发生错误。除了 @AndreKR 发布的缺少的分隔符之外,它对我来说看起来很好(使用字符串
à‚‚â£
进行测试)What is being returned from that preg_replace call, an unaffected string or NULL? If it's null, then an error has occured according to preg_replace manual . Other than the missing delimiters that @AndreKR posted, it looks fine to me (tested with the string
£
)