-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
68 lines (58 loc) · 2.42 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
function normalizeMobilePhoneNumber($phone, $isoCountryCode = "se")
{
$phoneFormat = array(
"se" => array(
"countryCode" => "46",
"phoneLength" => 9,
"startCharacters" => array("07"),
),
);
if (!array_key_exists(strtolower($isoCountryCode), $phoneFormat)) {
throw new Exception("iso country code does not exists");
}
$originalPhone = $phone;
$countryCode = $phoneFormat[strtolower($isoCountryCode)]["countryCode"];
$phoneLength = $phoneFormat[strtolower($isoCountryCode)]["phoneLength"];
$startCharacters = $phoneFormat[strtolower($isoCountryCode)]["startCharacters"];
$phone = preg_replace("/[\+\-()\s]/", "", $phone);
if ((strlen($phone) == $phoneLength + strlen($countryCode))) {
$phone = preg_replace("/^$countryCode/", "", $phone);
}
$phoneIncludeZeroBeginning = $phone;
$phone = preg_replace("/^0/", "", $phone);
if ((preg_match("/[^0-9]+/", $phone)) || (strlen($phone) > $phoneLength + strlen($countryCode))
|| ((strlen($phone) == $phoneLength + strlen($countryCode)) && (!preg_match("/^$countryCode/", $phone)))
|| (strlen($phone) < $phoneLength) || (strlen($phone) == 0)
|| ((count($startCharacters) > 0) && (!preg_match("/^(" . join("|", $startCharacters) .")/", $phoneIncludeZeroBeginning)))
) {
return array(
"original" => "$originalPhone",
"result" => "INVALID",
"explain" => "invalid phone number format",
"normalized" => "",
);
};
return array(
"original" => "$originalPhone",
"result" => "VALID",
"explain" => "it's mobile phone number",
"normalized" => "+{$countryCode}{$phone}",
);
}
$fileIn = fopen(realpath(dir(__FILE__)). DIRECTORY_SEPARATOR . "data/cellphones.txt", "r") or die("Unable to open file!");;
$fileOut = fopen(realpath(dir(__FILE__)). DIRECTORY_SEPARATOR . "data/cellphones_validate.txt", "w") or die("Unable to open file!");;
$outputDatas = array();
while(!feof($fileIn)){
$phone = fgetcsv($fileIn);
$outputDatas[] = normalizeMobilePhoneNumber( $phone[0],"se");
}
fclose($fileIn);
$count = 0;
fwrite($fileOut, "Order |original|result|explain|normalized\n");
foreach($outputDatas as $data){
$count++;
$line = "{$count}|{$data["original"]}|{$data["result"]}|{$data["explain"]}|{$data["normalized"]}\n";
fwrite($fileOut, $line);
}
fclose($fileOut);