检查 HTML 许多输入时preg_match错误



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

警告:preg_match() 期望参数 2 为字符串,数组在第 15 行的 C:\AppServ\www\regx1.php 中给出

 <?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!';
    }
        ?>

相关内容

最新更新