Skip to content

Commit 4169036

Browse files
committed
added limit() and fixed range()
1 parent 2d073f1 commit 4169036

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/VerbalExpressions/PHPVerbalExpressions/VerbalExpressions.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class VerbalExpressions
1515
public $suffixes = "";
1616
public $modifiers = "m"; // default to global multi line matching
1717
public $replaceLimit = 1; // the limit of preg_replace when g modifier is not set
18+
protected $lastAdded = false; // holds the last added regex
1819

1920
/**
2021
* Sanitize
@@ -41,7 +42,7 @@ public static function sanitize($value)
4142
*/
4243
public function add($value)
4344
{
44-
$this->source .= $value;
45+
$this->source .= $this->lastAdded = $value;
4546

4647
return $this;
4748
}
@@ -294,7 +295,7 @@ public function range()
294295
$arg_list = func_get_args();
295296

296297
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++]);
298299
}
299300

300301
$value .= "]";
@@ -500,4 +501,38 @@ public function clean($options = array())
500501
return $this;
501502
}
502503

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+
503538
}

0 commit comments

Comments
 (0)