forked from bnjunge/format-kenyan-phone-numbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdentifier.php
177 lines (169 loc) · 5.37 KB
/
Identifier.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
class Identifier{
/**
*
* Format phone numbers
* @param int :phone number
* @return int : formated phone number
* @return string : invalid error
* @access public
*/
public function formatted_phone_number($num)
{
$num = str_replace(array(" ", ",", ".", "!", "-", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_"), "", $num);
if (strlen($num) <= 12) {
$c = substr($num, 0, 1);
if (substr($num, 0, 3) == '254' and strlen($num) == 12) {
return $phone = $num;
} elseif ((strlen($num) == 10 || strlen($num) == 9) and ($c == 0 or $c == 7)) {
$phone = substr($num, -9);
$phone = '254' . $phone;
return $phone;
} else {
return 'Invalid Phone Number ' . $num;
}
}
}
/**
*
* List Country Telecoms with their assigned prefixes
* @version 1.0.0
* @author Benson Njunge <[email protected]>
*/
private function identifiers_imsi()
{
$countryCareers = array(
'kenya' => array(
'carriers' => array(
'Safaricom' => array(
'254101',
'254111',
'254700',
'254701',
'254702',
'254703',
'254704',
'254705',
'254706',
'254707',
'254708',
'254709',
'254710',
'254711',
'254712',
'254713',
'254714',
'254715',
'254716',
'254717',
'254718',
'254719',
'254720',
'254721',
'254722',
'254723',
'254724',
'254725',
'254726',
'254727',
'254728',
'254729',
'254790',
'254791',
'254792',
'254793',
'254797',
'254798',
'254799',
'254740',
'254741',
'254742',
'254743',
'254745',
'254746',
'254748',
'254757',
'254759',
'254769'
),
'Airtel' => array(
'254731',
'254732',
'254733',
'254734',
'254735',
'254736',
'254737',
'254738',
'254739',
'254750',
'254751',
'254752',
'254753',
'254754',
'254755',
'254756',
'254780',
'254781',
'254785',
'254786',
'254787',
'254788',
'254789'
),
'Telkom' => array(
'254770',
'254771',
'254772',
'254773',
'254774',
'254775',
'254776',
),
'Equitel' => array(
'254763',
'254764',
'254765'
),
'Faiba4G' => array(
'254747'
)
)
)
);
return $countryCareers;
}
/**
* Get Country ISPs
* @param int :phone number
* @return string ISP
* @access public
*/
public function check_operator($phone)
{
$prefix = substr($phone, 0, 6);
// load providers
$carrier = $this->identifiers_imsi();
$cc = json_decode(json_encode($carrier));
// Providers
$Safaricom = $cc->kenya->carriers->Safaricom;
$Airtel = $cc->kenya->carriers->Airtel;
$Telkom = $cc->kenya->carriers->Telkom;
$Equitel = $cc->kenya->carriers->Equitel;
$Faiba4G = $cc->kenya->carriers->Faiba4G;
// check if number is safaricom
if (in_array($prefix, $Safaricom)) {
return 'safaricom';
} elseif (in_array($prefix, $Airtel)) {
return 'airtel';
} elseif (in_array($prefix, $Telkom)) {
return 'telkom';
} elseif (in_array($prefix, $Equitel)) {
return 'equitel';
} elseif (in_array($prefix, $Faiba4G)) {
return 'faiba4g';
} else {
return 'Invalid Operator';
}
}
}