preg_replace 删除所有不需要的字符

preg_replace remove all unwanted characters

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

像 ᾄͭᾄ 这样的字符 或нєℓℓσ 你好 等..

我现在的代码是

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 修饰符您还需要扩展您的字符 class 以包含 non-ascii 个字符。

我会使用:

/[[:alnum:]]/u

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

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

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