在php中从字符串(简历)中提取电话号码

在php中从字符串(简历)中提取电话号码,php,string,extract,Php,String,Extract,需要从简历中提取电话号码。它可以以多种方式嵌入,也可以以自己的方式嵌入: 714-555-1212 或者排长队 1212 main st fullerton ca 92835 949.555.1212 要将其提取到数组中-区号,3位,4位 到目前为止,这就是我所拥有的,但当同一条线上除了电话号码之外还有其他号码时,它就不起作用了: function get_phone_number ($string){ $lines = explode("\n",trim($string));

需要从简历中提取电话号码。它可以以多种方式嵌入,也可以以自己的方式嵌入:

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;
}
函数获取电话号码($string){
$lines=分解(“\n”,修剪($string));
foreach($value形式的行){
//回显“行:.”值“
”; preg_match_all(“!\d+!”,$value,$matches); 如果(strlen($matches[0][0])='3'&&strlen($matches[0][1])='3'&&strlen($matches[0][2])=='4'){ $area_code=$matches[0][0]; } } 返回$area_代码; }
和?您想让我们为您编写吗?这不是phpfreaks(您将有更好的机会为您编写代码)。至少先试试。到目前为止,我已经有了这个方法,但当同一行上有其他号码时,比如街道地址数字,它就不起作用了:你只支持NANP格式的号码吗?当电话被点隔开时,它就起作用了。但有时它的(714)555-1212或714-555-1212或714 555-1212会根据您的说明编辑答案。必要时进行编辑,并使用or运算符。你应该能够从这个蓝图中找到它。谢谢,它工作得很好,除了它在这个-(760)617-7147上不起作用-我对正则表达式很不了解,需要添加什么以便它识别区号周围的括号?thanks@califmerchant阅读:如果你觉得这个答案有帮助,请点击✔ 就在它旁边。
<?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);