abod alabhawi
abod alabhawi

Reputation: 19

preg_replace remove all unwanted characters

i want to block or remove all unwanted characters from from my site

characters like ᾄҭᾄ or нєℓℓσ ĤĔĹĹŐ etc..

my code now is

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;
    }
}

its working but not bloking or removing characters like н Ĕ Ő

any idea ?

Upvotes: 1

Views: 222

Answers (1)

chris85
chris85

Reputation: 23892

The u modifier you will need to use you also need to expand your character class to include the non-ascii characters.

I'd use:

/[[:alnum:]]/u

Regex Demo: https://regex101.com/r/iS1yZ2/2

That is a posix bracket, you can see more of those here, www.regular-expressions.info/posixbrackets.html.

Also in your second expression the + needs to be escaped (or put in a character class, there are some symbols putting in a character won't fix -, ], ^) because that is a quantifier. There is a PHP function that will escape special characters, preg_quote.

Upvotes: 1

Related Questions