2014-02-13 72 views
0

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

<?php 
$convert = "mailto:[email protected]"; 

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] => [email protected]。请帮我实现这一点。

回答

0

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

$convert = "mailto:[email protected], mailto:[email protected]"; 
$finalstr = str_replace(array("mailto:", " "),"",$convert); 
$emailids = explode(",", $finalstr); 
var_dump($emailids); 
+0

给定的字符串将作为不同的电子邮件在整个字符串中重复。 –

+0

请举例.. –

+0

$ convert =“mailto:[email protected],mailto:[email protected]”; –

0
$convert = "mailto:[email protected]"; 

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:[email protected] 
) 
+0

我期待[0] => mailto:[email protected]。 –

+0

请检查更新的代码.. – Maion

0

下面应该为你做:

<?php 
//$convert = "mailto:[email protected]"; 
    $convert = 'mailto:[email protected], mailto:[email protected]'; 

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

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

更新与图案,干活g,并删除了多余的括号,工作: http://phpfiddle.org/main/code/3if-8qy

+0

谢谢。 $ convert =“mailto:[email protected],mailto:[email protected]”;我可以在数组中获得它吗? –

+0

删除模式外部的括号,以便在数组中不会有两倍的值:''/ mailto:。*(?:。com | .org | .net | .in){1}?/'' – Manu

+0

用于分隔逗号使用这种模式:''/ mailto:。+?@。+?(?:。com | .org | .net | .in){1}?/''你会得到一个包含我们的邮件的数组 – Manu

相关问题