如何在strpos中使用通配符


How to use wildcards in strpos

我有下面的脚本正在运行。我正在查找字符串"1/72"我还想寻找字符串"1:72"answers"1''72"-除了使用多个if语句之外,我该如何轻松地完成这项工作?)

$string="test string 1/72 PHP";
if (strpos($string, '1/72') > 0) {
        print "Got match!'n";
    } else {
        print "no match'n";
}

使用preg_match函数。

if (preg_match("~1['''':/]72~", $str)) {

与反斜杠或:或正斜杠匹配的['''':/]字符类。

演示

fnmatch()函数还提供了一种简单的方法来使用shell通配符表达式的模式匹配

$string="test string 1/72 PHP";
if (fnmatch('*1[/:'''']72*', $string)) {
    print "Got match!'n";
} else {
    print "no match'n";
}