Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching string with shell-style wildcards (e.g., *)

Is it possible to use a wildcard in an if statement?

My code:

* = wildcard

if ($admin =='*@some.text.here') {

}

$admin will be one of these:

like image 373
xvilo Avatar asked Mar 14 '13 15:03

xvilo


2 Answers

If you don't want to use regular expressions, fnmatch() might serve you well for this [limited] purpose. It matches strings using shell-like wildcards like you are expecting.

if (fnmatch('*@some.text.here', $admin)) {

}
like image 186
Wiseguy Avatar answered Oct 24 '22 23:10

Wiseguy


You could just check that the string ends with the value you are expecting:

$suffix = '@some.text.here';

if (substr($admin, -strlen($suffix)) == $suffix) {
    // Do something
}
like image 36
Sean Bright Avatar answered Oct 24 '22 23:10

Sean Bright