我有以下代码用于使用正则表达式规则匹配电子邮件地址。它运作良好,但我最近注意到它似乎与“空白”电子邮件地址匹配。if (preg_match("/.* <.*@.*\..*>/i",$this->to,$matches)) { $this->email_to = preg_replace("/.*<(.*)>.*/","$1",$this->to);} else { $this->email_to = $this->to;}我对 preg_match 的理解是:-查找任何字符,除了换行符<任何字符@anything.anything >不区分大小写?按照这些规则,如果有人可以提供一些指导,我无法完全弄清楚为什么它与空白/没有电子邮件地址匹配。谢谢你。
2 回答
守着星空守着你
TA贡献1799条经验 获得超8个赞
不需要preg_match + preg_replace。
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;
}
子衿沉夜
TA贡献1828条经验 获得超3个赞
我不知道它为什么会这样,但一个简单的解决方案是询问字符串是否为空白
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;
}
- 2 回答
- 0 关注
- 136 浏览
添加回答
举报
0/150
提交
取消
