dragon7713
2017-09-04 13:59
浏览 248
已采纳

使用strpos PHP检查多个变量?

I'm trying to make a simple search box that uses strposto check if the entered keyword makes a match with a variable. I have this working perfectly, however I can't seem to get it to work with multiple variables. Also I can't work out how to get it to output which variable has made the match.

I thought something along the lines of this would work for checking multiple variables but I was sadly mistaken:

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

If anyone can help here that would be great, this is the code I currently have working for one variable.

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>

图片转代码服务由CSDN问答提供 功能建议

我正在尝试创建一个简单的搜索框,使用 strpos 检查是否 输入的关键字与变量匹配。 我有这个完美的工作,但我似乎无法让它与多个变量一起工作。 此外,我无法弄清楚如何让它输出哪个变量进行了匹配。

我认为这样做可以用来检查多个变量但是我很遗憾地错了 :

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

如果有人可以 这里的帮助很棒,这是我目前为一个变量工作的代码。

PHP

 <  code>&lt;?
if(isset($ _ POST [“searchString”])){
 $ mystring1 ='你今天好吗'; 
 $ mystring2 ='你好,你叫什么名字'; 
 
 $  findme = $ _POST [“searchString”]; 
 $ pos = strpos($ mystring1,$ findme); 
 
 if if($ pos!== false){
 //找到
}其他{
  //找不到
} 
} 
?&gt; 
   
 
 

HTML

 &lt; html&gt; 
&lt; body&gt; 
&lt; form action =“test.php”method =“post”&gt; 
&lt; input type =“text”name =“searchString”&gt;  
&lt; / form&gt; 
&lt; / body&gt;  
&lt; / html&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanlun2827 2017-09-04 14:16
    已采纳

    You could do it like this.

    <?
    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 
        } 
    }
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题