doubanduo7620 2018-04-17 06:58
浏览 73
已采纳

找不到显示匹配? [重复]

This question already has an answer here:

        <?php
            $nameErr = "";
            if(isset($_POST["name"])){ //Check form submission
                $name = test_input($_POST["name"]);
                if (!preg_match("/^[a-zA-Z ]*$/",$name)) {//Check Validations
                   echo  "Only letters allowed"; 
                }
                elseif(empty($_POST["name"])){
                    echo "No letter entered";//Check not empty name
                }
                else { 
                    $veggies = array("Potato", "Cucumber", "Carrot", "Orange", "Green Beans", "onion");
                    $fruits  = array("Apple", "Banana", "orange", "Pineapple", "Grapes", "Watermelon");
                    $salad   = array_merge ($veggies, $fruits);
                    $Object = $_POST["name"];
                    $search = array_filter($salad, function($list) use ($Object){
                        return ( stripos($list, $Object) !== FALSE );
                    });
                    print_r($search); //displays the result
                } 

            }
            function test_input($data) {
                $data = htmlspecialchars($data);
                return $data;
            }
        ?>
        <!DOCTYPE HTML>
        <html>
            <body>
               <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                    Enter Letter: <input type="text" name="name"><span> <?php echo $nameErr;?></span><br><br>
                    <input type="submit" name="submit" value="Submit">
                </form>
            </body>
        </html>

what if i enter "aaaaaa" and such elements is not in the array, so then it should display match not found, i used in_array in isset statement but its not working , is it proper method to do or is it wrong?

</div>
  • 写回答

1条回答 默认 最新

  • douluoyou9876 2018-04-17 07:08
    关注
    <?php
        $nameErr = "";
        if(isset($_POST["name"])){ //Check form submission
        $name = test_input($_POST["name"]);
    
        if (!preg_match("/^[a-zA-Z ]*$/",$_POST["name"])) {//Check Validations
            echo  "Only letters allowed"; 
        } elseif(empty($_POST["name"])) {
            echo "No letter entered";//Check not empty name
        } else { 
          $veggies = array("Potato", "Cucumber", "Carrot", "Orange", "Green Beans", "onion");
          $fruits  = array("Apple", "Banana", "orange", "Pineapple", "Grapes", "Watermelon");
          $salad   = array_merge ($veggies, $fruits);
    
          if (in_array($_POST["name"], $salad)) {
                echo 'matches found';
           } else {
                echo 'matches not found';
           }
        }
    
    
    
         function test_input($data) {
                        $data = htmlspecialchars($data);
                        return $data;
         }
    ?>
    

    Try something like this

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line