php preg_match, matching when 2 words might come in random sequence

后端 未结 2 837
一个人的身影
一个人的身影 2021-01-24 06:57

Is it possible to match two words that might come in random sequences? Examples:

$title = \"2 pcs watch for couple\";
$title = \"couple watch 2 pcs\";

相关标签:
2条回答
  • 2021-01-24 07:19

    If you're just testing for the presence of the two words in the string you could use

    '/couple.*2 pcs|2 pcs.*couple/' 
    
    0 讨论(0)
  • 2021-01-24 07:23

    use strpos()

    if(strpos($string, '2 pcs') !== False){
     //its found
    }
    

    or matching at first and at end

    if(preg_match("/(^(2 pcs|couples)|(2 pcs|couples)$)/", '2 pcs watch for couples')){
    //echo "found";
    }
    

    or

    Matching anywhere:

    if(preg_match("/(2 pcs|couples)/", '2 pcs watch for couples')){
    //echo "found";
    }
    
    0 讨论(0)
提交回复
热议问题