preg_match_all (): Delimiter must not be alphanumeric or backslash

2
class Check {

    private static $Data;
    private static $Format;

    public static function Email($Email) {
        self::$Data = (string) $Email;
        self::$Format = "/[a-z0-9_\.\-]+@[a-z0-9_\.\-]*[a-z0-9_\.\-]+\.[a-z]{2,4}$/";

        if (preg_match(self::$Data, self::$Format)):
            return true;
        else:
            return false;
        endif;
    }

}

Up my code where I use, pre_match is giving error:

Delimiter must not be alphanumeric or backslash

I researched but could not solve. What am I doing wrong?

    
asked by anonymous 13.12.2016 / 23:33

1 answer

6

You are reversing the parameters, one read in the manual avoids these problems.

  

preg_match ( string $pattern , string $subject )

The correct order would be this:

 preg_match(self::$Format,self::$Data)

Enjoying, this does not make much sense:

if (preg_match(self::$Data, self::$Format)):
    return true;
else:
    return false;
endif;

If it is to return true or false already return the value directly, there is no reason for if .

class Check {

    private static $Data;
    private static $Format;

    public static function Email($Email) {
        self::$Data = (string) $Email;
        self::$Format = "/[a-z0-9_\.\-]+@[a-z0-9_\.\-]*[a-z0-9_\.\-]+\.[a-z]{2,4}$/";

        return preg_match(self::$Format,self::$Data);
    }
}

It is worth mentioning that the variables in this case could be local (method only, not class).

As Rafael mentioned in the comments, if you prefer PHP already has a ready alternative for this, which is recommended in place of your RegEx (which by the way does not respect the valid standard of emails, denying something it should not).

filter_input($string, FILTER_VALIDATE_EMAIL) 
    
13.12.2016 / 23:51