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
|
2874 |
|
public function __construct($pattern, $subject) |
34
|
|
|
{ |
35
|
2874 |
|
$this->pattern = str_replace('/', '\/', $pattern); |
36
|
2874 |
|
$this->subject = $subject; |
37
|
2874 |
|
} |
38
|
|
|
|
39
|
2874 |
|
protected function doMatch($type = 'find') |
40
|
|
|
{ |
41
|
2874 |
|
$final_pattern = '(?:' . $this->pattern . ')'; |
42
|
|
|
switch ($type) { |
43
|
2874 |
|
case 'matches': |
44
|
2869 |
|
$final_pattern = '^' . $final_pattern . '$'; |
45
|
2869 |
|
break; |
46
|
2832 |
|
case 'lookingAt': |
47
|
2827 |
|
$final_pattern = '^' . $final_pattern; |
48
|
2827 |
|
break; |
49
|
2813 |
|
case 'find': |
50
|
2813 |
|
default: |
51
|
|
|
// no changes |
52
|
2813 |
|
break; |
53
|
|
|
} |
54
|
2874 |
|
$final_pattern = '/' . $final_pattern . '/x'; |
55
|
2874 |
|
return (preg_match($final_pattern, $this->subject, $this->groups, PREG_OFFSET_CAPTURE) == 1) ? true : false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
2869 |
|
public function matches() |
62
|
|
|
{ |
63
|
2869 |
|
return $this->doMatch('matches'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
2827 |
|
public function lookingAt() |
70
|
|
|
{ |
71
|
2827 |
|
return $this->doMatch('lookingAt'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
2813 |
|
public function find() |
78
|
|
|
{ |
79
|
2813 |
|
return $this->doMatch('find'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
76 |
|
public function groupCount() |
86
|
|
|
{ |
87
|
76 |
|
if (empty($this->groups)) { |
88
|
|
|
return null; |
89
|
|
|
} else { |
90
|
76 |
|
return count($this->groups) - 1; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param int $group |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
34 |
|
public function group($group = null) |
99
|
|
|
{ |
100
|
34 |
|
if (!isset($group) || $group === null) { |
101
|
|
|
$group = 0; |
102
|
|
|
} |
103
|
34 |
|
return (isset($this->groups[$group][0])) ? $this->groups[$group][0] : null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param int|null $group |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
76 |
|
public function end($group = null) |
111
|
|
|
{ |
112
|
76 |
|
if (!isset($group) || $group === null) { |
113
|
76 |
|
$group = 0; |
114
|
76 |
|
} |
115
|
76 |
|
if (!isset($this->groups[$group])) { |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
76 |
|
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
|
3 |
|
} |
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
|
31 |
|
public function replaceFirst($replacement) |
138
|
|
|
{ |
139
|
31 |
|
return preg_replace('/' . $this->pattern . '/x', $replacement, $this->subject, 1); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $replacement |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
43 |
|
public function replaceAll($replacement) |
147
|
|
|
{ |
148
|
43 |
|
return preg_replace('/' . $this->pattern . '/x', $replacement, $this->subject); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $input |
153
|
|
|
* @return Matcher |
154
|
|
|
*/ |
155
|
5 |
|
public function reset($input = "") |
156
|
|
|
{ |
157
|
5 |
|
$this->subject = $input; |
158
|
|
|
|
159
|
5 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|