你好我想验证数组$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/