@@ -15,6 +15,7 @@ class VerbalExpressions
15
15
public $ suffixes = "" ;
16
16
public $ modifiers = "m " ; // default to global multi line matching
17
17
public $ replaceLimit = 1 ; // the limit of preg_replace when g modifier is not set
18
+ protected $ lastAdded = false ; // holds the last added regex
18
19
19
20
/**
20
21
* Sanitize
@@ -41,7 +42,7 @@ public static function sanitize($value)
41
42
*/
42
43
public function add ($ value )
43
44
{
44
- $ this ->source .= $ value ;
45
+ $ this ->source .= $ this -> lastAdded = $ value ;
45
46
46
47
return $ this ;
47
48
}
@@ -294,7 +295,7 @@ public function range()
294
295
$ arg_list = func_get_args ();
295
296
296
297
for ($ i = 0 ; $ i < $ arg_num ;) {
297
- $ value .= $ this ->sanitize ($ arg_list [$ i ++]) . " - " . $ this ->sanitize ($ arg_list [$ i ++]);
298
+ $ value .= $ this ->sanitize ($ arg_list [$ i ++]) . "- " . $ this ->sanitize ($ arg_list [$ i ++]);
298
299
}
299
300
300
301
$ value .= "] " ;
@@ -500,4 +501,38 @@ public function clean($options = array())
500
501
return $ this ;
501
502
}
502
503
504
+ /**
505
+ * Limit
506
+ *
507
+ * Adds char limit to the last added expression.
508
+ * If $max is less then $min the limit will be: At least $min chars {$min,}
509
+ * If $max is 0 the limit will be: exactly $min chars {$min}
510
+ * If $max bigger then $min the limit will be: at least $min but not more then $max {$min, $max}
511
+ *
512
+ * @access public
513
+ * @param integer $min
514
+ * @param integer $max
515
+ * @return VerbalExpressions
516
+ */
517
+ function limit ($ min , $ max = 0 ) {
518
+ if ($ max == 0 )
519
+ $ value = "{ " .$ min ."} " ;
520
+
521
+ else if ($ max < $ min )
522
+ $ value = "{ " .$ min .",} " ;
523
+
524
+ else
525
+ $ value = "{ " .$ min .", " .$ max ."} " ;
526
+
527
+ // check if the expression has * or + for the last expression
528
+ if (preg_match ("/\*|\+/ " , $ this ->lastAdded )) {
529
+ $ l = 1 ;
530
+ $ this ->source = strrev (str_replace (array ('+ ' ,'* ' ), strrev ($ value ), strrev ($ this ->source ), $ l ));
531
+ return $ this ;
532
+ }
533
+
534
+ return $ this ->add ($ value );
535
+
536
+ }
537
+
503
538
}
0 commit comments