This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
phone_number.php
77 lines (65 loc) · 1.92 KB
/
phone_number.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
<?php
$GLOBALS["carriers_number"] = [
'096' => 'Viettel',
'097' => 'Viettel',
'098' => 'Viettel',
'032' => 'Viettel',
'033' => 'Viettel',
'034' => 'Viettel',
'035' => 'Viettel',
'036' => 'Viettel',
'037' => 'Viettel',
'038' => 'Viettel',
'039' => 'Viettel',
'090' => 'Mobifone',
'093' => 'Mobifone',
'070' => 'Mobifone',
'071' => 'Mobifone',
'072' => 'Mobifone',
'076' => 'Mobifone',
'078' => 'Mobifone',
'091' => 'Vinaphone',
'094' => 'Vinaphone',
'083' => 'Vinaphone',
'084' => 'Vinaphone',
'085' => 'Vinaphone',
'087' => 'Vinaphone',
'089' => 'Vinaphone',
'099' => 'Gmobile',
'092' => 'Vietnamobile',
'056' => 'Vietnamobile',
'058' => 'Vietnamobile',
'095' => 'SFone'
];
/**
* Check if a string is started with another string
*
* @param string $needle The string being searched for.
* @param string $haystack The string being searched
* @return bool true if $haystack is started with $needle
*/
function start_with($needle, $haystack) {
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
/**
* Detect carrier name by phone number
*
* @param string $number The input phone number
* @return bool Name of the carrier, false if not found
*/
function detect_number ($number) {
$number = str_replace(array('-', '.', ' '), '', $number);
// $number is not a phone number
if (!preg_match('/^0[0-9]{9,10}$/', $number)) return false;
// Store all start number in an array to search
$start_numbers = array_keys($GLOBALS["carriers_number"]);
foreach ($start_numbers as $start_number) {
// if $start number found in $number then return value of $carriers_number array as carrier name
if (start_with($start_number, $number)) {
return $GLOBALS["carriers_number"][$start_number];
}
}
// if not found, return false
return false;
}