Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 允许空白记录的Preg_Php_Regex - Fatal编程技术网

Php 允许空白记录的Preg

Php 允许空白记录的Preg,php,regex,Php,Regex,我有下面使用正则表达式规则匹配电子邮件地址的代码 它工作得很好,但我最近注意到它似乎与一个“空白”电子邮件地址相匹配 if (preg_match("/.* <.*@.*\..*>/i",$this->to,$matches)) { $this->email_to = preg_replace("/.*<(.*)>.*/","$1",$this->to); } else { $this->email_to = $this->to; }

我有下面使用正则表达式规则匹配电子邮件地址的代码

它工作得很好,但我最近注意到它似乎与一个“空白”电子邮件地址相匹配

if (preg_match("/.* <.*@.*\..*>/i",$this->to,$matches)) {
  $this->email_to = preg_replace("/.*<(.*)>.*/","$1",$this->to);
} else {
  $this->email_to = $this->to;
}
if(预匹配(“/.*/i”,$this->to,$matches)){
$this->email\u to=preg\u replace(“/.*.*/”、“$1”、$this->to);
}否则{
$this->email_to=$this->to;
}
我对预赛的理解是:-

  • 查找除换行符以外的任何字符
  • 不区分大小写
  • 按照这些规则,如果有人能提供一些指导,我不太明白为什么它与空白/无电子邮件地址匹配


    谢谢。

    我不知道它为什么会这样,但一个简单的解决方法是询问字符串是否为空

    if (preg_match("/.* <.*@.*\..*>/i",$this->to,$matches)) {
      if ($matches != ""){
          $this->email_to = preg_replace("/.*<(.*)>.*/","$1",$this->to);
          } else { $this->email_to = $this->to; }
    } else {
      $this->email_to = $this->to;
    }
    
    if(预匹配(“/.*/i”,$this->to,$matches)){
    如果($matches!=“”){
    $this->email\u to=preg\u replace(“/.*.*/”、“$1”、$this->to);
    }else{$this->email_to=$this->to;}
    }否则{
    $this->email_to=$this->to;
    }
    
    我不知道它为什么会这样,但一个简单的解决方法是询问字符串是否为空

    if (preg_match("/.* <.*@.*\..*>/i",$this->to,$matches)) {
      if ($matches != ""){
          $this->email_to = preg_replace("/.*<(.*)>.*/","$1",$this->to);
          } else { $this->email_to = $this->to; }
    } else {
      $this->email_to = $this->to;
    }
    
    if(预匹配(“/.*/i”,$this->to,$matches)){
    如果($matches!=“”){
    $this->email\u to=preg\u replace(“/.*.*/”、“$1”、$this->to);
    }else{$this->email_to=$this->to;}
    }否则{
    $this->email_to=$this->to;
    }
    
    无需
    preg\u匹配
    +
    preg\u替换

    if (empty($this->to)) {
        $this->email_to = 'Is empty'; # assign what you want
    } elseif (preg_match("/<(.+?@.+?\..+?)>/", $this->to, $matches)) {
        $this->email_to = $matches[1];
    } else {
        $this->email_to = $this->to;
    }
    
    if(空($this->to)){
    $this->email_to='为空';#分配所需内容
    }elseif(预匹配(“/”,$this->to,$matches)){
    $this->email_to=$matches[1];
    }否则{
    $this->email_to=$this->to;
    }
    
    无需
    preg\u匹配
    +
    preg\u替换

    if (empty($this->to)) {
        $this->email_to = 'Is empty'; # assign what you want
    } elseif (preg_match("/<(.+?@.+?\..+?)>/", $this->to, $matches)) {
        $this->email_to = $matches[1];
    } else {
        $this->email_to = $this->to;
    }
    
    if(空($this->to)){
    $this->email_to='为空';#分配所需内容
    }elseif(预匹配(“/”,$this->to,$matches)){
    $this->email_to=$matches[1];
    }否则{
    $this->email_to=$this->to;
    }
    
    如果
    $this->to
    为空,您想做什么?如果记录为空,则跳过/放弃该记录。如果
    $this->to
    为空,您想做什么?如果记录为空,则跳过/放弃该记录,直到