从UTF8字符串中删除非字母数字字符的正则表达式


Regex to remove non alphanumeric characters from UTF8 strings

如何以多字节安全的方式从字符串中删除标点符号,逗号,破折号等字符?

我将处理来自许多不同语言的输入,我想知道是否有什么可以帮助我处理这个

谢谢

你可以使用unicode字符类:

  • http://www.regular-expressions.info/unicode.html
  • http://php.net/manual/en/regexp.reference.unicode.php

要匹配任何非字母符号,您只需使用'p{L}的负数'PL+。为了不删除空格,使用像[^'pL's]+这样的字符类。或者直接用'pP+

去掉标点符号

很明显,不要忘记正则表达式/u修饰符

我用了这个:

$clean = preg_replace( "/[^'p{L}|'p{N}]+/u", " ", $raw );
$clean = preg_replace( "/['p{Z}]{2,}/u", " ", $clean );

同类文章

从字符串

中删除非utf8字符

我不确定这是否涵盖了所有字符。

根据dreamincode论坛上的这篇文章

http://www.dreamincode.net/forums/topic/78179-regular-expression-to-remove-non-ascii-characters/

应该可以

/[^'x{21}-'x{7E}'s't'n'r]/

也许这将是有用的?

$newstring = preg_replace('/[^0-9a-zA-Z's]/', $oldstring);