去除utf中的垃圾字符
我使用 utf8 格式将所有数据存储到 mysql 中。在将数据插入数据库之前,我需要清理带有不需要的字符的字符串。字符串采用 utf8 格式。我知道如何使用正则表达式和字符串替换,但不知道如何使用阿拉伯字符。
需要清理的示例字符串:“████ .. ????????????????????????????????????????????????????????????????????????????????????????????????????????????
谢谢你
I am using utf8 format to store all my data into mysql. Before data is inserted into the database I need to clean the strings with unwanted characters. The strings are in utf8 format. I know how to use regex and string replace but do not know how to work with arabic characters.
Sample string that needs to be cleaned : "████ .. الــقــوانين الجديـــدة في قســـم الـعنايـ";
Thanking you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。正如 @Jonathan Leffler 已经说过的,如果您可以指定需要替换的字符的 unicode 字符范围,您可以使用正则表达式将字符替换为空字符串。
unicode 字符在表达式(在 PHP 中)中指定为
\x{FFFF}
。此外,您还必须设置u 修饰符
使 PHP 将模式视为 UTF8。
所以最后,你会得到这样的结果:
其中
/.../u
是分隔符加上修饰符[...]+
是字符类加上量词,这意味着 匹配这些字符一次或多次\x{FFFF}-\x{FFFF}
是一个 unicode 字符范围(显然你必须提供正确的代码点/字符数)。您还可以使用
^
来否定该组,您可以指定要保留的范围:更多信息:
Ok. As @Jonathan Leffler already said, if you can specify the unicode character ranges for the characters that need to be replaced, you can use a regular expression to replace the characters with an empty string.
A unicode character is specified as
\x{FFFF}
in an expression (in PHP). In addition, you have to set theu
modifier to make PHP treat the pattern as UTF8.So in the end, you have something like this:
where
/.../u
are the delimiters plus the modifier[...]+
is a character class plus quantifier, which means match any of these characters inside one or mor times\x{FFFF}-\x{FFFF}
is a unicode character range (obviously you have to provide the right codepoints/numbers of the characters).You can also negate the group with a
^
you can specify the range which you want to keep:More information: