2012-06-17 45 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

我編輯了答案以適合您的規格。在需要時編輯並使用or操作符。你應該能夠從這個藍圖中弄明白。 –

+0

謝謝,它工作得很好,除了它沒有在這工作 - (760)617-7147 - 我對正則表達式很不穩定,需要添加什麼才能識別區域代碼的括號?謝謝 – califmerchant