如何检查字符串是否包含电子邮件? [复制]

2024-01-22

我有这个代码来检查电子邮件是否有效:

$email = "[email protected] /cdn-cgi/l/email-protection"; 
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
if (preg_match($regex, $email)) {
 echo $email . " is a valid email. We can accept it.";
} else { 
 echo $email . " is an invalid email. Please try again.";
}

假设我有以下字符串:

<html>
<body>
<p>[email protected] /cdn-cgi/l/email-protection</p>
</body>
</html>

我如何检查该字符串是否包含电子邮件? (因为它不适用于上面的代码)


The ^ and $是锚点,需要完整的字符串匹配。如果你把它去掉,它将匹配字符串中某处的电子邮件。

正则表达式演示:https://regex101.com/r/xI0bC2/1 https://regex101.com/r/xI0bC2/1

PHP 用法(已更新以存储找到的电子邮件):

<?php
$email = "<html>
<body>
<p>[email protected] /cdn-cgi/l/email-protection</p>
</body>
</html>"; 
$regex = '/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/'; 
if (preg_match($regex, $email, $email_is)) {
 echo $email_is[0] . " is a valid email. We can accept it.";
} else { 
 echo $email . " is an invalid email. Please try again.";
}

PHP 演示:https://eval.in/618679 https://eval.in/618679

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何检查字符串是否包含电子邮件? [复制] 的相关文章

随机推荐