Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_match Error When Check HTML many inputs

Tags:

html

css

php

mysql

Hello I want to verify that all the fields in the array $key in which the first letter is M, but there is a problem which is:

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>

.

like image 775
Mostafa Avatar asked Dec 30 '25 09:12

Mostafa


1 Answers

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

or

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

        ?>
like image 80
Firewizz Avatar answered Jan 01 '26 22:01

Firewizz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!