完美的英语白名单cherecters reg表达php

时间:2012-09-06 10:34:26

标签: php regex parsing encoding

我的问题是我解析了很多文本段落,并且在其他语言中总是有奇怪的符号和字母。

我只需要用英语解析文本。

我只想构建一个很好的,很酷的reg表达式,只保留:

1)英文字母
2)数字
3)键盘上的字符就像!@#$等。
4)所有常见的标志,如商标等。
5)我是否错过了可能出现在文本中并且相关的任何字符?

非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以使用此功能(方法),来自Nette Framework

/**
 * Converts to ASCII.
 * @param  string  UTF-8 encoding
 * @return string  ASCII
 */
public static function toAscii($s)
{
    $s = preg_replace('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{2FF}\x{370}-\x{10FFFF}]#u', '', $s);
    $s = strtr($s, '`\'"^~', "\x01\x02\x03\x04\x05");
    if (ICONV_IMPL === 'glibc') {
        $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); // intentionally @
        $s = strtr($s, "\xa5\xa3\xbc\x8c\xa7\x8a\xaa\x8d\x8f\x8e\xaf\xb9\xb3\xbe\x9c\x9a\xba\x9d\x9f\x9e"
            . "\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3"
            . "\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8"
            . "\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe",
            "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
    } else {
        $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); // intentionally @
    }
    $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
    return strtr($s, "\x01\x02\x03\x04\x05", '`\'"^~');
}