我有一个模式列表:$allowedTLDS = array(".de", ".at", ".ch", ".org", ".com", ".eu");
现在我有像 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="093a393b383b3f3b3b646860654964706768646c276a6664" rel="noreferrer noopener nofollow">[email protected]</a>
这样的字符串我想剪掉 .com 之后的所有内容以获得普通邮件地址。
到目前为止我已经编写了这个函数:
function extract_correct_email_address($string){
$allowedTLDS = array(".de", ".at", ".ch", ".org", ".com", ".eu");
$foundMatch = false;
$foundTLD = "";
foreach ($allowedTLDS as $tld) {
if (strpos($string, $tld) !== FALSE) {
//found a match
$foundMatch = true;
$foundTLD = $tld;
break 1;
}
}
if($foundMatch){
$str = strtok( $string, $foundTLD).$foundTLD;
return $str;
}
return NULL;
}
现在我有例如这些地址,我可以在右侧获得输出:
input: [email protected] output: [email protected] expected: [email protected] (this one is correct) input: [email protected] output: [email protected] expected: [email protected] input: [email protected] expected: [email protected]
我希望人们可以理解我想要存档的内容。我在函数中做错了什么?
最佳答案
使用这个漂亮的正则表达式来实现 - 您只需添加 TLD:
function extract_correct_email_address($string){
// pattern do match email addresses
$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.(de|at|ch|org|com|eu)(?:\.[a-z]{2})?/i';
preg_match($pattern, $string, $matches);
if(count($matches[0])<1) {
return null;
}
return $matches[0];
}
// example string
$string = 'fdsaf das D <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d454c435e446d59485e590349485a5a5a0341424103434859" rel="noreferrer noopener nofollow">[email protected]</a> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="680e1a090612280f010a1c1b46091c0c091b0e" rel="noreferrer noopener nofollow">[email protected]</a> dasf asd';
var_dump(extract_correct_email_address($string));
结果:
string(15) "[email protected]"
当然,您可以扩展它以用于提取多封电子邮件。而不是 matches[0]
检查所有匹配项。
关于php - 删除php中字符串模式之后的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47202742/