forked from VerbalExpressions/JSVerbalExpressions
/
VerbalExpressions.php
354 lines (311 loc) · 7.83 KB
/
VerbalExpressions.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* Verbal Expressions v0.1 (https://github.com/jehna/VerbalExpressions) ported in PHP
* @author Mihai Ionut Vilcu (ionutvmi@gmail.com)
* 22.July.2013
*/
// // some tests
// $regex = new VerEx;
// $regex ->startOfLine()
// ->then( "http" )
// ->maybe( "s" )
// ->then( "://" )
// ->maybe( "www." )
// ->anythingBut( " " )
// ->endOfLine();
// if($regex->test("http://github.com"))
// echo "valid url";
// else
// echo "invalid url";
// if (preg_match($regex, 'http://github.com')) {
// echo 'valid url';
// } else {
// echo 'invalud url';
// }
// echo "<pre>". $regex->getRegex() ."</pre>";
// echo $regex ->clean(array("modifiers"=> "m","replaceLimit"=>4))
// ->find(' ')
// ->replace("This is a small test http://somesite.com and some more text.", "-");
class VerEx {
var $prefixes = "";
var $source = "";
var $suffixes = "";
var $modifiers = "m"; // default to global multiline matching
var $replaceLimit = 1; // the limit of preg_replace when g modifier is not set
/**
* Sanitation function for adding anything safely to the expression
* @param string $value the to be added
* @return string escaped value
*/
function sanitize( $value ) {
if(!$value)
return $value;
return preg_quote($value, "/");
}
/**
* Add stuff to the expression
* @param string $value the stuff to be added
*/
function add( $value ) {
$this->source .= $value;
return $this;
}
/**
* Mark the expression to start at the beginning of the line.
* @param boolean $enable Enables or disables the line starting. Default value: true
*/
function startOfLine( $enable = true ) {
$this->prefixes = $enable ? "^" : "";
return $this;
}
/**
* Mark the expression to end at the last character of the line.
* @param boolean $enable Enables or disables the line ending. Default value: true
*/
function endOfLine( $enable = true ) {
$this->suffixes = $enable ? "$" : "";
return $this;
}
/**
* Add a string to the expression
* @param string $value The string to be looked for
*/
function then( $value ) {
$this->add("(".$this->sanitize($value).")");
return $this;
}
/**
* alias for then()
* @param string $value The string to be looked for
*/
function find( $value ) {
return $this->then($value);
}
/**
* Add a string to the expression that might appear once (or not).
* @param string $value The string to be looked for
*/
function maybe( $value ) {
$this->add("(".$this->sanitize($value).")?");
return $this;
}
/**
* Accept any string
*/
function anything() {
$this->add("(.*)");
return $this;
}
/**
* Anything but this chars
* @param string $value The unaccepted chars
*/
function anythingBut( $value ) {
$this->add("([^". $this->sanitize($value) ."]*)");
return $this;
}
/**
* Accept any non-empty string
*/
function something() {
$this->add("(.+)");
return $this;
}
/**
* Anything non-empty except for these chars
* @param string $value The unaccepted chars
*/
function somethingBut( $value ) {
$this->add("([^". $this->sanitize($value) ."]+)");
return $this;
}
/**
* Shorthand for preg_replace()
* @param string $source the string that will be affected(subject)
* @param string $value the replacement
*/
function replace($source, $value) {
// php doesn't have g modifier so we remove it if it's there and we remove limit param
if(strpos($this->modifiers, 'g') !== false){
$this->modifiers = str_replace('g', '', $this->modifiers);
return preg_replace($this->getRegex(), $value, $source);
}
return preg_replace($this->getRegex(), $value, $source, $this->replaceLimit);
}
/**
* Match line break
*/
function lineBreak() {
$this->add("(\\n|(\\r\\n))");
return $this;
}
/**
* Shorthand for lineBreak
*/
function br() {
return $this->lineBreak();
}
/**
* Match tabs.
*/
function tab() {
$this->add("\\t");
return $this;
}
/**
* Match any alfanumeric
*/
function word() {
$this->add("\\w+");
return $this;
}
/**
* Any of the listed chars
* @param string $value The chars looked for
*/
function anyOf( $value ) {
$this->add("[". $value ."]");
return $this;
}
/**
* Shorthand for anyOf
* @param string $value The chars looked for
*/
function any( $value ) {
return $this->anyOf($value);
}
/**
* Adds a range to our expresion ex: range(a,z) => a-z, range(a,z,0,9) => a-z0-9
*/
function range() {
$arg_num = func_num_args();
if($arg_num%2 != 0)
throw new Exception("Number of args must be even", 1);
$value = "[";
$arg_list = func_get_args();
for($i = 0; $i < $arg_num;)
$value .= $this->sanitize($arg_list[$i++]) . " - " . $this->sanitize($arg_list[$i++]);
$value .= "]";
$this->add($value);
return $this;
}
/**
* Adds a modifier
*/
function addModifier( $modifier ) {
if(strpos($this->modifiers, $modifier) === false)
$this->modifiers .= $modifier;
return $this;
}
/**
* Removes a modifier
*/
function removeModifier( $modifier ) {
$this->modifiers = str_replace($modifier, '', $modifier);
return $this;
}
/**
* Match case insensitive or sensitive based on $enable value
* @param boolean $enable Enables or disables case sensitive. Default true
*/
function withAnyCase( $enable = true ) {
if($enable)
$this->addModifier('i');
else
$this->removeModifier('i');
return $this;
}
/**
* Toggles g modifier
* @param boolean $enable Enables or disables g modifier. Default true
*/
function stopAtFirst( $enable = true ) {
if($enable)
$this->addModifier('g');
else
$this->removeModifier('g');
return $this;
}
/**
* Toggles m modifier
* @param boolean $enable Enables or disables m modifier. Default true
*/
function searchOneLine( $enable = true ) {
if($enable)
$this->addModifier('m');
else
$this->removeModifier('m');
return $this;
}
/**
* Adds the multiple modifier at the end of your expresion
* @param string $value Your expresion
*/
function multiple( $value ) {
$value = $this->sanitize($value);
switch (substr($value, -1)) {
case '+':
case '*':
break;
default:
$value += '+';
break;
}
$this->add($value);
return $this;
}
/**
* Wraps the current expresion in an `or` with $value
* @param string $value new expression
*/
function _or( $value ) {
if(strpos($this->prefixes, "(") === false)
$this->prefixes .= "(";
if(strpos($this->suffixes, ")") === false)
$this->suffixes .= ")";
$this->add(")|(");
if($value)
$this->add($value);
return $this;
}
/**
* PHP Magic method to return a string representation of the object.
*
* @return string
*/
public function __toString() {
return $this->getRegex();
}
/**
* Creates the final regex.
* @return string The final regex
*/
function getRegex() {
return "/".$this->prefixes . $this->source . $this->suffixes. "/" . $this->modifiers;
}
/**
* tests the match of a string to the current regex
* @param string $value The string to be tested
* @return boolean true if it's a match
*/
function test($value) {
// php doesn't have g modifier so we remove it if it's there and call preg_match_all()
if(strpos($this->modifiers, 'g') !== false){
$this->modifiers = str_replace('g', '', $this->modifiers);
return preg_match_all($this->getRegex(), $value);
}
return preg_match($this->getRegex(), $value);
}
/**
* deletes the current regex for a fresh start
*/
function clean($options = array()) {
$options = array_merge(array("prefixes"=> "", "source"=>"", "suffixes"=>"", "modifiers"=>"gm","replaceLimit"=>"1"), $options);
$this->prefixes = $options['prefixes'];
$this->source = $options['source'];
$this->suffixes = $options['suffixes'];
$this->modifiers = $options['modifiers']; // default to global multiline matching
$this->replaceLimit = $options['replaceLimit']; // default to global multiline matching
return $this;
}
}