Skip to content

Instantly share code, notes, and snippets.

@hejrobin
Created April 23, 2012 14:45
Show Gist options
  • Save hejrobin/2471367 to your computer and use it in GitHub Desktop.
Save hejrobin/2471367 to your computer and use it in GitHub Desktop.
I was super bored. So I made this.
<?php
abstract class Type {
protected $type;
public abstract function isValid();
public function getType() {
return $this->type;
}
}
class RegExp extends Type {
const REGEX_PATTERN_VALIDATE = '/^(?:([^[:alpha:]\\\\{<\[\(])(.+)(?:\1))|(?:{(.+)})|(?:<(.+)>)|(?:\[(.+)\])|(?:\((.+)\))[gimsxu]*$/';
protected $type = 'regexp';
protected $regexp;
public function __construct($regexp) {
$this->regexp = $regexp;
}
public function isValid() {
return preg_match(self::REGEX_PATTERN_VALIDATE, $this->regexp);
}
public function getRegExp() {
if($this->isValid() === true) {
return $this->regexp;
}
return null;
}
public function match($string) {
preg_match($this->regexp, $string, $matches);
return $matches;
}
public function matchAll($string) {
preg_match_all($this->regexp, $string, $matches);
return $matches;
}
public function test($string) {
$matches = count($this->match($string));
if($matches > 0) {
return true;
}
return false;
}
public function split($string, $limit = -1, $flags = 0) {
return preg_split($this->regexp, $string, $limit, $flags);
}
public function replace($string, $replace, $limit = -1) {
return preg_replace($this->regexp, $replace, $string, $limit);
}
public function __toString() {
return $this->getRegExp();
}
}
$regex = new RegExp('/(hello)/');
var_dump(
$regex->test('hello Robin')
);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment