php preg_match, matching when 2 words might come in random sequence
问题 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"; Currently I'm using two regexes: if (preg_match("/2( )?pcs/i", $title) and preg_match("/couple/i", $title)) Just want to know if it can be done with only 1? 回答1: If you're just testing for the presence of the two words in the string you could use '/couple.*2 pcs|2 pcs.*couple/' 回答2: use strpos() if(strpos($string, '2 pcs') !== False){ //its found }