|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace libphonenumber; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Matcher for various regex matching |
|
7
|
|
|
* |
|
8
|
|
|
* Note that this is NOT the same as google's java PhoneNumberMatcher class. |
|
9
|
|
|
* This class is a minimal port of java's built-in matcher class, whereas PhoneNumberMatcher |
|
10
|
|
|
* is designed to recognize phone numbers embedded in any text. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Matcher |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $pattern; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $subject; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $groups = array(); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $pattern |
|
31
|
|
|
* @param string $subject |
|
32
|
|
|
*/ |
|
33
|
2827 |
|
public function __construct($pattern, $subject) |
|
34
|
|
|
{ |
|
35
|
2827 |
|
$this->pattern = str_replace('/', '\/', $pattern); |
|
36
|
2827 |
|
$this->subject = $subject; |
|
37
|
2827 |
|
} |
|
38
|
|
|
|
|
39
|
2827 |
|
protected function doMatch($type = 'find') |
|
40
|
|
|
{ |
|
41
|
2827 |
|
$final_pattern = '(?:' . $this->pattern . ')'; |
|
42
|
|
|
switch ($type) { |
|
43
|
2827 |
|
case 'matches': |
|
44
|
2821 |
|
$final_pattern = '^' . $final_pattern . '$'; |
|
45
|
2821 |
|
break; |
|
46
|
2787 |
|
case 'lookingAt': |
|
47
|
2784 |
|
$final_pattern = '^' . $final_pattern; |
|
48
|
2784 |
|
break; |
|
49
|
2767 |
|
case 'find': |
|
50
|
|
|
default: |
|
51
|
|
|
// no changes |
|
52
|
2767 |
|
break; |
|
53
|
|
|
} |
|
54
|
2827 |
|
$final_pattern = '/' . $final_pattern . '/x'; |
|
55
|
2827 |
|
return (preg_match($final_pattern, $this->subject, $this->groups, PREG_OFFSET_CAPTURE) == 1) ? true : false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
2821 |
|
public function matches() |
|
62
|
|
|
{ |
|
63
|
2821 |
|
return $this->doMatch('matches'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
2784 |
|
public function lookingAt() |
|
70
|
|
|
{ |
|
71
|
2784 |
|
return $this->doMatch('lookingAt'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
2767 |
|
public function find() |
|
78
|
|
|
{ |
|
79
|
2767 |
|
return $this->doMatch('find'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return int |
|
84
|
|
|
*/ |
|
85
|
70 |
|
public function groupCount() |
|
86
|
|
|
{ |
|
87
|
70 |
|
if (empty($this->groups)) { |
|
88
|
|
|
return null; |
|
89
|
|
|
} else { |
|
90
|
70 |
|
return count($this->groups) - 1; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param int $group |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
33 |
|
public function group($group = null) |
|
99
|
|
|
{ |
|
100
|
33 |
|
if (!isset($group)) { |
|
101
|
|
|
$group = 0; |
|
102
|
|
|
} |
|
103
|
33 |
|
return (isset($this->groups[$group][0])) ? $this->groups[$group][0] : null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param int|null $group |
|
108
|
|
|
* @return int |
|
109
|
|
|
*/ |
|
110
|
70 |
|
public function end($group = null) |
|
111
|
|
|
{ |
|
112
|
70 |
|
if (!isset($group) || $group === null) { |
|
113
|
70 |
|
$group = 0; |
|
114
|
|
|
} |
|
115
|
70 |
|
if (!isset($this->groups[$group])) { |
|
116
|
|
|
return null; |
|
117
|
|
|
} |
|
118
|
70 |
|
return $this->groups[$group][1] + strlen($this->groups[$group][0]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
public function start($group = null) |
|
122
|
|
|
{ |
|
123
|
3 |
|
if (isset($group) || $group === null) { |
|
124
|
3 |
|
$group = 0; |
|
125
|
|
|
} |
|
126
|
3 |
|
if (!isset($this->groups[$group])) { |
|
127
|
|
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
3 |
|
return $this->groups[$group][1]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $replacement |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
30 |
|
public function replaceFirst($replacement) |
|
138
|
|
|
{ |
|
139
|
30 |
|
return preg_replace('/' . $this->pattern . '/x', $replacement, $this->subject, 1); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $replacement |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
42 |
|
public function replaceAll($replacement) |
|
147
|
|
|
{ |
|
148
|
42 |
|
return preg_replace('/' . $this->pattern . '/x', $replacement, $this->subject); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $input |
|
153
|
|
|
* @return Matcher |
|
154
|
|
|
*/ |
|
155
|
4 |
|
public function reset($input = "") |
|
156
|
|
|
{ |
|
157
|
4 |
|
$this->subject = $input; |
|
158
|
|
|
|
|
159
|
4 |
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|