2014-02-13 6 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) {1}?/''mailto의 – Manu