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/

相关文章:

mysql - 将索引与范围类型查询和其他条件一起使用

mysql - 从 ubuntu 机器移动 mysql 成功,但内存消耗来自旧目录

php - 为什么我的 SQL 插入语句无法通过我的 php 脚本执行?

php - 按天登记出勤

javascript - 是否可以将多个输入字段值存储在一个 session 存储中并使用 session 存储在第二页中检索?

php - 在 PHP 中创建动态类别菜单

Mysql 安装程序显示错误 : Memoy could not be written

php - 如何在 codeigniter Controller 中定义 json header ?

javascript - 如何将函数中找到的javascript变量值存储在php变量中

html - 没有绝对位置的图像上的 CSS 文本(宽度 100%)