从 PHP 中的字符串(简历)中提取电话号码


extract phone number from string (resume) in 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;
}
<?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);