dongmei2957
2015-02-25 10:44
浏览 55
已采纳

如何在strpos中使用通配符

I have the below script which is working. I am looking for the string '1/72' I would also like to look for the string '1:72' and '1\72' - how would I go about doing this easily (apart from just using multiple if statements?)

$string="test string 1/72 PHP";

if (strpos($string, '1/72') > 0) {
        print "Got match!
";
    } else {
        print "no match
";
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyin8809 2015-02-25 10:46
    已采纳

    Use preg_match function.

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

    [\\\\:/] character class which matches a backslash or : or forward slash.

    DEMO

    已采纳该答案
    打赏 评论
  • doue8385 2015-02-25 11:06

    The fnmatch() function also provides a simple way to use pattern matching with shell wildcard expressions

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

相关推荐 更多相似问题