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
为空,您想做什么?如果记录为空,则跳过/放弃该记录,直到