2015-11-16 69 views
1

我試圖使輸入不是模式。我如何preg_match多個單詞

$input= "words trying to compare"; 

preg_match("/\b{$input}\b/", "these are a lot of words that i'm trying to compare") 
       // ^this might contain multiple words, but the same as ^^ these 
echo "match is found"; 

我怎樣才能使它的,即使輸入單詞不在相同的順序一句我試圖把它比,它可以返回「發現匹配。」

回答

1

只需使用preg_match_all

preg_match_all("/\b{$input}\b/", "these are a lot of words that i'm trying to compare",$matchedWords); 

多個單詞:

<?php 
$input= "words compare"; 
$pattern = str_replace(" ","|",$input); 
preg_match_all("/{$pattern}/", "these are a lot of words that i'm trying to compare words",$matchedWords); 
       // ^this might contain multiple words, but the same as ^^ these 
var_dump($matchedWords); 
+0

我只是想這一點,當輸入爲「單詞進行比較」(以錯誤的順序搜索它)沒有關係沒有任何回報。 – Simba

+0

查看更新的答案 – Thamilan

+0

謝謝,工作得很好。 – Simba

0
$input= "words|trying|to|compare"; 
$count = preg_match_all('#\b('.$input.')\b#', $string, $matches); 

if($count) 
echo 'found word'; 
else 
echo 'not found';