2014-02-13 70 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

相關問題