电子邮件正则表达PHP

时间:2014-02-13 11:27:33

标签: php regex mailto

我有一个字符串,我需要从该字符串解析的电子邮件ID。我已经使用PHP正则表达式进行检索并且工作正常。但我的问题是电子邮件前缀是否包含正则表达式。

<?php
$convert = "mailto:xxxin@yahoo.inATTENDEE";

preg_match_all('/mailto:(.*?)(.com|.org|.net|.in)/', $convert, $emails);

echo "<pre>";
print_r($emails);
echo "</pre>";
?>

输出:

阵 (

[0] => Array
    (
        [0] => mailto:xxxin
    )

[1] => Array
    (
        [0] => xx
    )

[2] => Array
    (
        [0] => xin
    )

但我期待[0] => mailto.xxxin@yahoo.in。请帮助我实现这一目标。

3 个答案:

答案 0 :(得分:0)

只需使用str_replace()explode()作为:

$convert    = "mailto:xxx@yahoo.com, mailto:xxxin@yahoo.in";
$finalstr   = str_replace(array("mailto:", " "),"",$convert);
$emailids   = explode(",", $finalstr);
var_dump($emailids);

答案 1 :(得分:0)

$convert = "mailto:xxxin@yahoo.inATTENDEE";

preg_match_all('/(mailto.*(?:.com|.org|.net|.in){1}?)/', $convert, $emails);

$newArray = array();
foreach($emails as $em){
$newArray = array_merge($newArray, $em);
break;
}

echo "<pre>";
print_r($newArray);
echo "</pre>";

结果

Array
(
    [0] => mailto:xxxin@yahoo.in
)

答案 2 :(得分:0)

以下内容应该为您完成:

<?php
//$convert = "mailto:xxxin@yahoo.inATTENDEE";
    $convert = 'mailto:xxx@yahoo.com, mailto:xxxin@yahoo.in';

preg_match_all('/mailto:.*?(?:\.com|\.org|\.net|\.in){1}/', $convert, $emails);

echo "<pre>";
print_r($emails);
echo "</pre>";
?>

更新了该模式,工作并删除了无关的括号,正在运行: http://phpfiddle.org/main/code/3if-8qy