Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php regex中/name/i的含义是什么_Php_Regex - Fatal编程技术网

Php regex中/name/i的含义是什么

Php regex中/name/i的含义是什么,php,regex,Php,Regex,我有一个php文件,可以插入用户电话号码并检查数据是否存在 <?php $labels=array("first_name"=>"First Name", "last_name"=>"Last Name", "phone"=>"Phone"); ?> <body> <?php foreach($_POST as $field =>$value) { if(empty($v

我有一个php文件,可以插入用户电话号码并检查数据是否存在

<?php
$labels=array("first_name"=>"First Name",
                "last_name"=>"Last Name",
                "phone"=>"Phone");
?>
<body>
<?php
foreach($_POST as $field =>$value)
{
    if(empty($value))
    {
        $blank_array[]=$field;
    }
    elseif(preg_match("/name/i",$field))
    {
        if(!preg_match("/^[A-Za-z' -]{1,50}$/",$value))
        {
            $bad_format[]=$field;
        }
    }
    elseif($field=="phone")
    {
        if(!preg_match("/^(\(\d+\)|\d+\-)?\d{10,20}$/",$value))
        {
            $bad_format[]=$field;
        }
    }
}

if(@sizeof($blank_array)>0 or  @sizeof($bad_format)>0)
{
    if(@sizeof($blank_array)>0)
    {
        echo "<p>input";
        foreach($blank_array as $value)
        {
        echo " $labels[$value]";
        }
        echo "</p>";
    }

    if(@sizeof($bad_format)>0)
    {
        echo "<p>invalid format";
        foreach($bad_format as $value)
        {
            echo $labels[$value];
        }
        echo "</p>";
    }

//redisplay form
    echo "<hr/>";
    echo "enter phone number";
    echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";

    foreach($labels as $field =>$label)
    {
        $good_data[$field]=strip_tags(trim($_POST[$field]));
        echo "$label <input type='text' name='$field' size='65' maxlength='65' value='$good_data[$field]'/><br/>";  
    }
    echo "<input type='submit' value='submit phone number'/>";
exit();
}
else //check wether use exist or not
{   
    $user='root';
    $host='localhost';
    $password='root';
    $dbname='pet';
    $cxn=mysqli_connect($host,$user,$password,$dbname) or die("can't connect to server");
    foreach($labels as $field =>$value) //filter data 
        {
            $good_data[$field]=strip_tags(trim($_POST[$field]));
            $good_data[$field]=mysqli_real_escape_string($cxn,$good_data[$field]);
        }
    $check_exist="SELECT "; //loop the fields
    //create an array to store the field
    $fieldArray = array();
    foreach($labels as $field =>$value)
    {
        $fieldArray[] = $field;
    }
    $check_exist .= join(',', $fieldArray);
    $check_exist.=" FROM data WHERE "; //loop the value and create an array to store values
    $whereArray = array();
    foreach($good_data as $field =>$value)
    {       
        if($field=="phone")
        {
        $value=preg_replace("/(\(\d+\)|\d+\-)/","",$value);
        }
         $whereArray[] = $field . "=" . "'$value'";
    }
    $check_exist .= join(' AND ', $whereArray);

    $result=mysqli_query($cxn,$check_exist);
    if(mysqli_num_rows($result))
    {
        echo "user already exist ! $check_exist";
        echo "<hr/>";
        echo "enter phone number";
        echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
        foreach($labels as $field =>$label)
        {
            $good_data[$field]=strip_tags(trim($_POST[$field]));
            echo "$label <input type='text' name='$field' size='65' maxlength='65' value='$good_data[$field]'/><br/>";  
        }
        echo "<input type='submit' value='submit phone number' />";
        exit();
    }
    else
    {
        foreach($labels as $field =>$value)
        {
            $good_data[$field]=strip_tags(trim($_POST[$field]));
                if($field=="phone")
                {
                    $good_data[$field]=preg_replace("/(\(\d+\)|\d+\-)/","",$good_data[$field]);
                }
            $good_data[$field]=mysqli_real_escape_string($cxn,$good_data[$field]);
        }
        $query="INSERT INTO data ("; //118
        foreach($good_data as $field =>$value) //119
        {
            $query.="$field,"; // dau phay lien ket vs ") cua line 86
        }
        $query.= ") VALUES (";          //123
        $query=preg_replace("/,\)/",")",$query); //124 remove the comma that was inserted after the last field remove the ,) with )
        foreach($good_data as $field =>$value)   //124
        {
            $query.="'$value',";
        }
        $query.=")";
        $query=preg_replace("/,\)/",")",$query);


        $result=mysqli_query($cxn,$query) or die ("can't execute query.".mysqli_error($cxn));
        echo "$query";
        echo "<h4>member inserted $query </h4>";
    }
}

?>
</body>


/name/i
查找
name
,但不区分大小写,因为
i
。还有其他几个修饰符。

/name/i
查找
name
,但不区分大小写,因为
i
。还有其他几个修改器

preg_match("/name/i",$field)
preg_match
检查变量
$field
是否匹配不区分大小写的模式
name

这样,如果
$字段
包含
名称
名称
名称
等,则
if
将计算为true,程序进入if块

preg_match
检查变量
$field
是否匹配不区分大小写的模式
name


因此,如果
$field
包含
Name
Name
Name
等,则
if
将计算为true,程序将进入if块。

php.net是一个很好的资源。试试这个页面:php.net是一个很好的资源。请尝试此页面: