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

使用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>
  • 写回答

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 
        } 
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度