forked from eturino/Util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phone.php
59 lines (49 loc) · 1.82 KB
/
Phone.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
<?php
class EtuDev_Util_Phone
{
static public function isSpanishStrictPhoneNumber($value, $accept_intel_phone = false, $accept_mobile = true)
{
if ($accept_mobile) {
$pattern = '/^[5,6,7,8,9]{1}\d{8}$/';
$pattern_prefix = '/^\+34[5,6,7,8,9]{1}\d{8}$/';
} else {
$pattern = '/^[8,9]{1}\d{8}$/';
$pattern_prefix = '/^\+34[8,9]{1}\d{8}$/';
}
if (preg_match($pattern, $value) || preg_match($pattern_prefix, $value) || $value == '') {
if (!$accept_intel_phone && preg_match('/^[9,8]0\d{7}$/', $value)) {
return false;
}
return true;
} else {
return false;
}
}
static public function formatSpanishPhone($lPhone)
{
// 93 345 31 45
// 983 47 18 21
$prefix = '';
$lPhone = trim($lPhone);
if ($lPhone && self::isSpanishStrictPhoneNumber($lPhone, true, true)) {
if (EtuDev_Util_String::beginsWith($lPhone, '+34')) {
$lPhone = substr($lPhone, 3);
$prefix = '(+34) ';
}
if (EtuDev_Util_String::strlen($lPhone) == 9) {
if (EtuDev_Util_String::beginsWith($lPhone, '93') || EtuDev_Util_String::beginsWith($lPhone, '91')) {
return $prefix . substr($lPhone, 0, 2) . ' ' . substr($lPhone, 2, 3) . ' ' . substr($lPhone, 5, 2) . ' ' . substr(
$lPhone,
7
);
} else {
return $prefix . substr($lPhone, 0, 3) . ' ' . substr($lPhone, 3, 2) . ' ' . substr($lPhone, 5, 2) . ' ' . substr(
$lPhone,
7
);
}
}
}
return $lPhone;
}
}