preg_replace删除所有不需要的字符


preg_replace remove all unwanted characters

我想从我的网站中阻止或删除所有不需要的字符

像 ᾄᾄ᭭ᾄ或 нєllσĤĔĹĹŐ等。。

我现在的代码是

class badWordsC
{
    public function check($text)
    {
        $badwords    = 'com|net|org|info|.name|.biz|.me|.tv|.tel|.mobi|.asia|.uk|.eu|.us|.in|.tk|.cc|.ws|.bz|.mn|.co|.tw|.vn|.es|.pw|.club|.ca|.cn|.email|.photography|.photos|.tips|.solutions|.center|.gallery|.kitchen|.land|.technology|.today|.academy|.computer|.shoes|.careers|.domains|.coffee|.link|.guru|.estate|.company|.bike|.clothing|.holdings|.plumbing|.singles|.ventures|.camera|.equipment|.graphics|.lighting|.construction|.contractors|.directory|.diamonds|.enterprises|.voyage|.recipes|.gift|.site|.ly|.gq|.cf|.ga|.ml|.tk|in|rb2';
        $badwords   .= 'type|ingoogle';
        $badwords    = explode('|', $badwords);
        $goodwords   = 'youtube.com|prntscr.com|az545221.vo.msecnd.net';
        $goodwords  .= 'wink|crying|fingerscrossed|blushing|wondering|inlove|evilgrin|yawning|puking|in';
        $goodwords   = explode('|', $goodwords);

        $text        = str_replace($goodwords, '', $text);
        $text        = trim(preg_replace('/'s's+/', '', $text));
        $text        = preg_replace('/'P{L}+/u', '', $text);


        foreach ($badwords as $word)
        {
            if (strpos($text, $word) !== false || strpos($text, strtoupper($word)) !== false)
            {
                return false;
            }
        }
        $text = preg_replace("/[a-zA-Z0-9]/", '', $text);
        $text = preg_replace(array('/)/','/(/','/;/','/-/','/+/','/لأ/','/لإ/','/لا/','/إ/','/أ/', '/ا/', '/ض/', '/ص/', '/ث/', '/ق/', '/ف/', '/غ/', '/ع/', '/ه/', '/خ/', '/ح/', '/ج/', '/د/', '/ش/', '/س/', '/ي/', '/ب/', '/ل/', '/ت/', '/ن/', '/م/', '/ك/', '/ط/', '/ئ/', '/ء/', '/ؤ/', '/ر/', '/ى/', '/ة/', '/و/', '/ز/', '/ظ/', '/ذ/', '/ـ/'), '', $text);

        if($text != '')
        {
            return false;
        }
        return true;
    }
}

它有效但不膨胀或删除字符,如 н Ĕ Ő

知道吗?

您需要

使用的u修饰符 您还需要扩展字符类以包含非 ASCII 字符。

我会使用:

/[[:alnum:]]/u

正则表达式演示:https://regex101.com/r/iS1yZ2/2

这是一个 posix 括号,你可以在这里看到更多这样的,www.regular-expressions.info/posixbrackets.html。

同样在你的第二个表达式中,+需要被转义(或者放在一个字符类中,有一些符号放入一个字符不会修复-]^),因为这是一个量词。有一个PHP函数可以转义特殊字符,preg_quote。