Skip to content

Commit

Permalink
update db source and add mcc/mnc
Browse files Browse the repository at this point in the history
  • Loading branch information
calibr committed Dec 25, 2015
1 parent bdc3dcd commit 5645625
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 46 deletions.
2 changes: 1 addition & 1 deletion db

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions fetch-db.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
<?php

$url = "https://www.ip2location.com/mobile-carrier-coverage";
$url = "http://mcc-mnc.com/";

$html = file_get_contents($url);

$rows = preg_match_all(
'@<tr><td style="text-align:center;">([A-Z]{2})</td><td>(.+?)</td><td style="text-align:center;">(.+?)</td><td style="text-align:right;">[0-9,.]+?</td></tr>@i',
'@<tr><td>([0-9]+)</td><td>([0-9]+)</td><td>([a-z]+)</td><td>.+?</td><td>[0-9]+</td><td>(.+?)</td></tr>@i',
$html, $matches, PREG_SET_ORDER
);

$db = [];

foreach($matches as $match) {
$country = $match[1];
$carrier = $match[3];
$country = strtoupper($match[3]);
$mcc = (int)$match[1];
$mnc = (int)$match[2];
$carrier = trim($match[4]);
if(!isset($db[$country])) {
$db[$country] = [];
}
$db[$country][] = $carrier;
$db[$country][] = [
"name" => $carrier,
"mcc" => $mcc,
"mnc" => $mnc
];
}

file_put_contents("db", serialize($db));
38 changes: 10 additions & 28 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,19 @@ $carriers = CountryMobileCarriers::getList("RU");
/**
* carriers array will look like this:
* [
* "Baykalwestcom",
* "Beeline",
* "INDIGO",
* "MOTIV",
* "MTS",
* "MegaFon",
* "Skylink",
* "Tele2",
* "Yota"
* ]
* [
* "name" => "Baykal Westcom",
* "mcc" => 250,
* "mnc" => 12
* ],
* ...
* ]
*/

// returns whole database as array, key is country code, value is list of carriers
// in same format as in getList
$allCarriers = CountryMobileCarriers::getAll();

/**
* allCarriers array will look like this:
* [ "RU" => [
* "Baykalwestcom",
* "Beeline",
* "INDIGO",
* "MOTIV",
* "MTS",
* "MegaFon",
* "Skylink",
* "Tele2",
* "Yota"
* ]
* ...
* ]
*/

```

Database is taken from https://www.ip2location.com/mobile-carrier-coverage.
Database is taken from http://mcc-mnc.com/.
19 changes: 7 additions & 12 deletions tests/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@

class CountryMobileCarriersTest extends PHPUnit_Framework_TestCase
{
public function testGetForRU() {
public function testGetList() {
$carriers = CountryMobileCarriers::getList("RU");
$this->assertArraySubset([
"Baykalwestcom",
"Beeline",
"INDIGO",
"MOTIV",
"MTS",
"MegaFon",
"Skylink",
"Tele2",
"Yota"
], $carriers);
$carrier = $carriers[0];
var_dump($carrier);
$this->assertInternalType("string", $carrier["name"]);
$this->assertInternalType("int", $carrier["mcc"]);
$this->assertInternalType("int", $carrier["mnc"]);
$this->assertCount(22, $carriers);
}
}

0 comments on commit 5645625

Please sign in to comment.