2012-06-17 3 views
-6

이력서에서 전화 번호를 추출해야합니다. 그것은 다양한 방법으로 내장 될 수 있으며, 한 줄 일 수 있습니다 PHP에서 문자열 (이력서)에서 전화 번호 추출

714-555-1212 

또는 긴 줄

1212 main st fullerton ca 92835 949.555.1212 

에서

배열로 추출하고 싶습니다 - 지역 코드 3 자리, 4 자리 숫자

이 내가 지금까지 무엇을 가지고 있지만, 전화 번호 외에 같은 줄에 다른 숫자가있을 때 작동 나던 :

function get_phone_number ($string){ 
    $lines = explode("\n",trim($string)); 
    foreach ($lines as $value){ 
     //echo "Line: ".$value."<BR>"; 

     preg_match_all('!\d+!', $value, $matches); 
     if (strlen($matches[0][0])=='3' && strlen($matches[0][1])=='3' && strlen($matches[0][2])=='4') { 
      $area_code = $matches[0][0]; 

     } 
    } 
    return $area_code; 
} 
+1

그리고? 우리가 당신을 위해 그것을 쓰길 원하십니까? – xbonez

+2

이것은 phpfreaks가 아닙니다 (그 중 귀하가 작성한 코드가 더 나을 가능성이 큽니다). 적어도 먼저 시도하십시오. –

+0

나는 지금까지 이것을 가지고 있지만, 같은 줄에 거리 번호와 같은 다른 번호가있을 때는 작동하지 않습니다. – califmerchant

답변

2
<?php 
$areaCodes = array(); 
$threeDigits = array(); 
$fourDigits = array(); 

preg_match_all('/(\d{3})-(\d{3})-(\d{4})/',$content,$matches); 

if(count($matches[1])>0) 
{ 
    for($i=0; $i<count($matches[1]); $i++) 
    { 
     array_push($areaCodes,$matches[1][$i]); 
     array_push($threeDigits,$matches[2][$i]); 
     array_push($fourDigits,$matches[3][$i]);  
    } 
} 

preg_match_all('/\((\d{3})\)-(\d{3})-(\d{4})/',$content,$matches); 

if(count($matches[1])>0) 
{ 
    for($i=0; $i<count($matches[1]); $i++) 
    { 
     array_push($areaCodes,$matches[1][$i]); 
     array_push($threeDigits,$matches[2][$i]); 
     array_push($fourDigits,$matches[3][$i]);  
    } 
} 
preg_match_all('/(\d{3}).(\d{3}).(\d{4})/',$content,$matches); 

if(count($matches[1])>0) 
{ 
    for($i=0; $i<count($matches[1]); $i++) 
    { 
     array_push($areaCodes,$matches[1][$i]); 
     array_push($threeDigits,$matches[2][$i]); 
     array_push($fourDigits,$matches[3][$i]);  
    } 
} 

preg_match_all('\((\d{3})\).(\d{3}).(\d{4})/',$content,$matches); 

if(count($matches[1])>0) 
{ 
    for($i=0; $i<count($matches[1]); $i++) 
    { 
     array_push($areaCodes,$matches[1][$i]); 
     array_push($threeDigits,$matches[2][$i]); 
     array_push($fourDigits,$matches[3][$i]);  
    } 
} 
print_r($areaCodes); 
print_r($threeDigits); 
print_r($fourDigits); 
+0

은 전화 번호가 점으로 구분되어있을 때 작동합니다. 하지만 때때로 (714) 555-1212 또는 714-555-1212 또는 714 555-1212 – califmerchant

+0

귀하의 사양에 맞는 대답을 편집했습니다. 필요한 경우 편집하고 또는 연산자를 사용하십시오. 이 청사진에서 파악할 수 있어야합니다. –

+0

고마워, 이걸 제외하고는 꽤 잘 돌아 간다. (760) 617-7147 - 정규 표현식에 꽤 불안해하고, 지역 코드 주위의 괄호를 인식 할 수 있도록 추가해야하는 것은 무엇인가? 감사합니다 – califmerchant