Php 如何正确存储和查找电话号码中的区号

Php 如何正确存储和查找电话号码中的区号,php,mysql,phone-number,Php,Mysql,Phone Number,如何在数据库中存储区号(npa nxx)以进行快速查找 这里的交易:我有一个变量,其中包含一个电话号码,我需要在数据库中查找附加到该电话号码的城市 问题是,不同的国家有不同的格式 Canada/USA: +19055551234 (+1 > Country, 905 > Area Code, 555 > City Code) France: +33512345678 (+33 > Country, 5 > Areacode, 1 > City, Other n

如何在数据库中存储区号(npa nxx)以进行快速查找

这里的交易:我有一个变量,其中包含一个电话号码,我需要在数据库中查找附加到该电话号码的城市

问题是,不同的国家有不同的格式

Canada/USA: +19055551234 (+1 > Country, 905 > Area Code, 555 > City Code)
France: +33512345678 (+33 > Country, 5 > Areacode, 1 > City, Other numbers > Subscriber number)
等等(基于维基百科的信息)

我创建了一个名为“npanxx”的表,其中包含每个表所附的区号、城市代码和城市列表(带有国家id和省/州id):

我正在考虑以下程序:

Get all country codes from sql to php array
Go through each entry and check if there's a match from the beginning of the phone number
When (If there's) a match is found
  Remove the beginning of the phone number
  Get all npa-nxx that belong to that contry and put them in a php array
  Go through each value of the array to find a matching beginning
  When (If there's) a match is found
    Remove the beginning of the phone number
    Store data in different variables like: $country = 'Canada'; $city = 'Acton Vale'...
    etc, etc.
第一个错误(我认为):太多数据库请求(npanxx表只包含加拿大一个省的3000条记录) 第二个错误:我很确定没有必要检查每一个npa nxx代码

另一个问题是:不确定如果电话号码是法国号码,这个程序是否有效

而且。。。如果有一个条目,比如说336,另一个条目是3364,它可能给出错误的结果

你知道我怎样才能解决这个问题吗?(我不要求任何代码,我不想为我做这项工作,但我想得到一些线索)


这是一个为加拿大多发性硬化症协会捐款的人事项目,我真的很想完成这个项目:)

我想可能是某种形式的注册码或其他模式匹配,以方便您在搜索方面进行选择。这是我用PHP编写的一个小脚本,用于从area-codes.com实时返回NPA/NXX作为JSON对象

它返回一些非常有用的数据。这只适用于NANP,所以它在识别国际电话方面做得不太好。为此,我建议制作一个表格,列出所有国际国家代码以及在国际上拨打这些代码的适当方法

此外,网络交换运营商需要一个国际拨号码(如美国的011,或一般手机的+),以确定该号码是否为国际号码,然后按照上述步骤确定您要去哪里。您可以将此约束添加到输入字段中,并使用它完成操作

但是,如果您只是想在北美编号计划中获取NPA/NXX信息,那么这个脚本应该非常有用

顺便说一句,area-codes.com将在线查询计算在其免费服务中,我在网站上没有发现任何迹象表明该代码违反了该政策。但这段代码可以重新配置,以便从其他提供者那里收集数据,尽管如此

对于您的示例npanxx.php?npa=450&nxx=236返回的数据有点有限,因为它是加拿大,加拿大不像美国那样提供所有FIP和运营商数据,但返回的数据仍然非常有用:

{
"npaareacode":"450",
"nxxusetype":"WIRELESS",
"nxxprefix":"236",
"nxxintroversion":"2002-08-04",
"city":"ACTON VALE",
"state":"QC",
"latitude":"45.6523",
"longitude":"-72.5671",
"countypopulation":"51400",
"lata":"850",
"zipcodecount":"0",
"zipcodefreq":"-1",
"observesdst":"Unknown",
"timezone":"Eastern (GMT -05:00)"
}

谢谢你的建议!竖起大拇指,我会试着考虑那个可能性。
 <?php

    // Small script to return and format all data from the NPA/NXX info site www.area-codes.com
    // Returns a JSON object.

    error_reporting(E_NONE);

    $npa = $_GET['npa'];
    $nxx = $_GET['nxx'];

    function parseInput($input) {

        $v = new DOMDocument();

        $v->formatOutput = true;
        $v->preserveWhiteSpace = false;

        $v->loadHTML($input);

        $list = $v->getElementsByTagName("td");

        $e = false;
        $dataOut = array();
        $p = "";

        foreach($list as $objNode) {
            if (!$e) {
                $p = $objNode->nodeValue;

                $p = strtolower($p);
                $p = preg_replace("%[+ .:()\/_-]%", "", $p);
                $p = str_replace("\xc2\xa0", "", $p);
                $p = trim($p);
            }
            else {
                if ($p != "") {
                    $d = trim($objNode->nodeValue);
                    if ($d != "") $dataOut[$p] = $d;
                }
                $p = "";
            }

            $e = !$e;  
        }

        return $dataOut;

    }

    function getNPANXX($npa, $nxx) {

        $url = "www.area-codes.com/exchange/exchange.asp?npa=$npa&nxx=$nxx";
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch, CURLOPT_URL, $url);

        $response = curl_exec($ch);
        curl_close($ch);

        $i = strpos($response, "<h3>AreaCode/Prefix $npa-$nxx Details</h3>");
        $i = strpos($response, "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">", $i);
        $e = strpos($response, "</table>", $i);

        $scan = substr($response, $i, ($e-$i) + 8);
        return parseInput($scan);

    }

    $result = getNPANXX($npa, $nxx);

    if (!isset($result['npaareacode'])) {
        $result = array("error" => "invalid");
    }

    echo json_encode($result);
    die;

?>
{
"npaareacode":"202",
"nxxusetype":"WIRELESS",
"nxxprefix":"520",
"nxxintroversion":"11\/16\/2007",
"city":"WASHINGTON",
"state":"DC",
"latitude":"38.901",
"county":"DISTRICT OF COLUMBIA",
"longitude":"-77.0315",
"countypopulation":"0",
"lata":"236",
"zipcode":"20005",
"zipcodecount":"0",
"ratecenter":"WSHNGTNZN1",
"zipcodefreq":"0",
"fips":"11001",
"ocn":"6664",
"observesdst":"Unknown",
"cbsacode":"47900",
"timezone":"Eastern (GMT -05:00)",
"cbsaname":"Washington-Arlington-Alexandria, DC-VA-MD-WV",
"carriercompany":"SPRINT SPECTRUM L.P."
}
{
"npaareacode":"450",
"nxxusetype":"WIRELESS",
"nxxprefix":"236",
"nxxintroversion":"2002-08-04",
"city":"ACTON VALE",
"state":"QC",
"latitude":"45.6523",
"longitude":"-72.5671",
"countypopulation":"51400",
"lata":"850",
"zipcodecount":"0",
"zipcodefreq":"-1",
"observesdst":"Unknown",
"timezone":"Eastern (GMT -05:00)"
}