php - 检查 HTML 许多输入时出现 preg_match 错误

标签 php html mysql css

你好我想验证数组$key中第一个字母为M的所有字段,但是有一个问题是:

Warning: preg_match() expects parameter 2 to be string, array given in C:\AppServ\www\regx1.php on line 15

 <?php
    if (isset($_POST['zr'])){
        $pattern = "/^m/";
     $key = array($_POST['text'],$_POST['text1'],$_POST['text2'],$_POST['text3']);
        if (preg_match($pattern,$key))

        echo 'is Mathcing M is First!';
    }else {

        echo "M it's Not First!'";
    }

    ?>

    <form action="regx1.php" method="post">
    <input type="submit" name="zr" />
    <br />
    <hr />
    <input type="text" name="text" /><br />
    <input type="text" name="text1" /><br />
    <input type="text" name="text2" /><br />
    <input type="text" name="text3" /><br />
    </form>

.

最佳答案

<?php
if (isset($_POST['zr']))
{
     $pattern = "/^m/";
     $key = array($_POST['text'],$_POST['text1'],$_POST['text2'],$_POST['text3']);

    foreach($key as $val)
    {      
        if (preg_match($pattern,$val))
        {
            echo 'is Mathcing M is First!';
        }
        else 
        {
            echo "M it's Not First!'";
        }
}

<?php
if (isset($_POST['zr']))
{
         $pattern = "/^m/";
         $key = array($_POST['text'],$_POST['text1'],$_POST['text2'],$_POST['text3']);

        $failed = false;
        foreach($key as $val)
        {      
            if (!preg_match($pattern,$val))
                $failed = true;
            }
        }

    if ($failed)
    {
        echo "M it's Not First!'";
    }
    else
    {
        echo 'is Mathcing M is First!';
    }

        ?>

关于php - 检查 HTML 许多输入时出现 preg_match 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35460859/

相关文章:

php - 在函数中包含sql查询

php - 理解 php 中的继承

jquery - 通过 AJAX 发布 Excel 文件

javascript - 在Reactjs中管理状态时遇到麻烦

mysql - 如何对数据表的每个表值求和

c# - 在 .NET 的 MySQL 查询中使用 CONCAT

php - 执行查询时,我在 wampserver MySql 5.7.14 中收到与 sql_mode=only_full_group_by 相关的错误

javascript - 下拉列表中未选择值,请将内联 js 代码转换为外部 j 查询

php - 从外部服务器更新 php 应用程序

javascript - 输入类型 ="button"的 onClick 函数不起作用