Corresponding string with shell wildcards (e.g. *)

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

My code is:

*= wildcard

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

}

$admin will be one of the following:

  • xvilo@some.text.here
  • bot!bot@some.text.here
  • lakjsdflkjasdflkj@some.text.here
+5
source share
5 answers

If you do not want to use regular expressions, it fnmatch()may serve you well for this [limited] purpose. It matches strings using shell-like wildcards, as you would expect.

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

}
+13
source

You can simply check if the string ends with the expected value:

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

if (substr($admin, -strlen($suffix)) == $suffix) {
    // Do something
}
+3
source

.
*, . ( ).

:
*xxx - "xxx"
xxx* - "xxx"
xx*zz - "xx" "zz"
*xx* - "xx"

function wildcard_match($pattern, $subject)
{
    $pattern='/^'.preg_quote($pattern).'$/';
    $pattern=str_replace('\*', '.*', $pattern);
    //$pattern=str_replace('\.', '.', $pattern);
    if(!preg_match($pattern, $subject, $regs)) return false;
    return true;
}
if (wildcard_match('*@some.text.here', $admin)) {

}

preg_match() .

+3
if (strstr ($admin,"@some.text.here")) {

}

strstr(), , , stristr()

strpos -

$pos = strrpos($mystring, "@some.text.here");
if ($pos === false) { // note: three equal signs
    // not found...
} else {
    //found
}

( , )

$checkstring = "@some.text.here";
$pos = strrpos($mystring, $checkstring, -(strlen($checkstring)));
if ($pos === false) { // note: three equal signs
    // not found...
} else {
    //found
}
+1

, , strpos():

if (strpos($admin, '@some.test.here') !== false) { }

, @some.text.here , substr_compare() .

if (substr_compare($str, $test, strlen($str)-strlen($test), strlen($test)) === 0) {}
0
source

All Articles