<?php
final class Regexp {
private $matches;
private $regexp;
/**
* Regexp class constructor
*
* @param string regular expression without modifiers
* @param integer modifiers to the regular expression
* @return object Regexp object
*/
public function __construct($regexp, $modifiers) {
$this->regexp = '/' . $regexp . '/';
if($modifiers & REGEXP_CASE_INSENSITIVE) $this->regexp .= 'i';
if($modifiers & REGEXP_DOT_ALL) $this->regexp .= 's';
if($modifiers & REGEXP_IGNORE_WHITESPACE) $this->regexp .= 'x';
if($modifiers & REGEXP_MULTILINE) $this->regexp .= 'm';
if(!($modifiers & REGEXP_NO_ANALYSE)) $this->regexp .= 'S';
if($modifiers & REGEXP_UNGREEDY) $this->regexp .= 'U';
if($modifiers & REGEXP_UTF8) $this->regexp .= 'u';
}
/**
* Checks whether the regular expression operation ended successfully or not
*
* @param mixed regular expression operation return value
* @return mixed regular expression operation return value
* @throws exception if regular expression operation ended with an error
*/
private function checkResult($result) {
$code = preg_last_error();
if($code != PREG_NO_ERROR || $result === false || $result === NULL) {
switch($code) {
case PREG_BACKTRACE_LIMIT_ERROR:
$message = 'Backtrack limit exhausted';
break;
case PREG_BAD_UTF8_ERROR:
$message = 'Bad UTF-8 encoding';
break;
case PREG_INTERNAL_ERROR:
$message = 'Internal REGEXP error';
break;
case PREG_RECURSION_LIMIT_ERROR:
$message = 'Recursion limit exhausted';
break;
default:
$message = 'Unknown REGEXP error';
break;
}
$error = sprintf('Could not execute regular expression (%s): %s', $this->regexp, $message); throw new Throwable\Regexp($error);
}
return $result;
}
/**
* Performs a regular expression search and replace
*
* @param mixed string or an array with strings to replace
* @param mixed string or an array with strings to search and replace
* @return mixed array if $subject id an array, or string otherwise
* @throws exception if regular expression operation ended with an error
*/
public function filter($replacement, $subject) {
return $this->checkResult(preg_filter($this->regexp, $replacement, $subject));
}
/**
* Returns the global Regexp object
*
* @param string regular expression without modifiers
* @param integer modifiers to the regular expression
* @return object Regexp object
*/
public static function create
($regexp, $modifiers = REGEXP_MODIFIER_NONE
) { return new Regexp($regexp, $modifiers);
}
/**
* Returns the matches of the last regular expression operation
*
* @return array results of last regular expression operation
*/
public function getMatches() {
return $this->matches;
}
/**
* Returns the compiled regular expression
*
* @return string compiled regular expression
*/
public function getRegexp() {
return $this->regexp;
}
/**
* Returns an array of entries that match the pattern
*
* @param array input array
* @param integer combined by the bitwise operator flags passed to preg_grep()
* @return array array consisting of the elements that matches the regular expression
* @throws exception if regular expression operation ended with an error
*/
public function grep($subject, $flags = 0) {
return $this->checkResult(preg_grep($this->regexp, $subject, $flags)); }
/**
* Checks whether the regular expression is syntactically correct
*
* @return boolean TRUE if regular expression has correct syntax, or FALSE otherwise
*/
public function isValid() {
try {
$this->match();
}
catch(Throwable\Regexp $e) {
return false;
}
return true;
}
/**
* Performs a regular expression match
*
* @param string the input string
* @param boolean specifies whether to search for all occurences or just to find the first one
* @param integer combined by the bitwise operator flags passed to preg_match()
* @return integer the number of full pattern matches (which might be zero)
* @throws exception if regular expression operation ended with an error
*/
public function match($subject, $all = false, $flags = 0) {
if($all) {
return $this->checkResult(preg_match_all($this->regexp, $subject, $this->matches, $flags)); }
return $this->checkResult(preg_match($this->regexp, $subject, $this->matches, $flags)); }
/**
* Performs a regular expression search and replace
*
* @param mixed string or an array with strings to replace
* @param mixed string or an array with strings to search and replace
* @return mixed array if $subject id an array, or string otherwise
* @throws exception if regular expression operation ended with an error
*/
public function replace($replacement, $subject) {
if(is_callable($replacement)) {
}
return $this->checkResult(preg_replace($this->regexp, $replacement, $subject)); }
/**
* Splits a string by the regular expression
*
* @param string the input string
* @param integer combined by the bitwise operator flags passed to preg_split()
* @return array array containing substrings split along boundaries matched by regular expression
* @throws exception if regular expression operation ended with an error
*/
public function split($subject, $flags = 0
) { return $this->checkResult(preg_split($this->regexp, $subject, NULL, $flags)); }
} /* class */
?>