2017-09-04 62 views
1

我正試圖做一個簡單的搜索框,使用strpos來檢查輸入的關鍵字是否與變量匹配。我有這個完美的工作,但我似乎無法讓它與多個變量一起工作。我也無法弄清楚如何讓它輸出哪個變量進行匹配。用strpos檢查多個變量PHP?

我想沿着這個東西線將用於檢查多個變量的工作,但我是大錯特錯了:

$pos = strpos($mystring1, $mystring2, $findme); 

如果任何人都可以在這裏幫助將是巨大的,這是我現在有工作的代碼一個變量。

PHP

<? 
if(isset($_POST["searchString"])) { 
    $mystring1 = 'how are you today'; 
    $mystring2 = 'hello what is your name'; 

    $findme = $_POST["searchString"]; 
    $pos = strpos($mystring1, $findme); 

    if ($pos !== false) { 
     //found 
    } else { 
     //not found 
    } 
} 
?> 

HTML

<html> 
    <body> 
     <form action="test.php" method="post"> 
      <input type="text" name="searchString"> 
     </form> 
    </body> 
</html> 
+1

它可以是數組而不是變量嗎? https://stackoverflow.com/questions/6932438/search-for-partial-value-match-in-an-array – chris85

+1

或https://stackoverflow.com/a/34365357/2263631 – Script47

+0

strpos的第3個參數是帶偏移量的整數。 http://php.net/manual/en/function.strpos.php –

回答

0

你可以不喜歡這樣。

<? 
if(isset($_POST["searchString"])) { 
    $mystring1 = 'how are you today'; 
    $mystring2 = 'hello what is your name'; 

    $findme = $_POST["searchString"]; 
    $pos = strpos($mystring1, $findme); 
    $pos2 = strpos($mystring2, $findme); 

    if ($pos !== false && $pos2 !== false) { 
     //found in both strings 
    } else if ($pos !== false || $pos2 !== false) { 
     //found in 1 of the 2 strings 
    } else { 
     //not found 
    } 


    if ($pos !== false) { 
     //found in string 1 
    } 
    if ($pos2 !== false) { 
     //found in string 2 
    } 
} 
?> 
+0

這工作得很好,謝謝你。有沒有辦法使這項工作,因此它不區分大小寫? –

+0

strtolower()你正在compilation的兩個變量。 mb_strtolower()在你使用unicode進行比較的兩個變量上。 –

+0

感謝它完美的工作! –