<?php
function RC($flags = ""){
return new RegCode($flags);
}
static class RegCodeFn {
public static function escape($s){
return preg_replace("/[\\/\\\\\\^\\[\\]\\.\\$\\{\\}\\*\\+\\(\\)\\|\\?\\<\\>]/", "\\\\$0", $s);
}
public static function compileArr($chain){
$l = count($chain);
$code = "";
for ($i=0; $i < $l; $i++) {
$code .= RegCodeFn::compile($chain[$i],($i + 1) < $l ? $chain[$i+1]['type'] == 'repeat' : false);
}
return $code;
}
public static function compile($item, $repeatNext){
switch ($item['type']) {
case 'range':
return '[' . ($item['not']?'^':'') . $item['allowed'] . ']';
break;
case 'group':
$res = RegCodeFn::compileArr($item['chain']);
return '(' . ($item['assertion']?:'') . ($item['name']?(($item['assertion']?'':'?') . 'P<' . $item['name'] . '>' ):'') . $res['code'] . ')';
break;
case 'raw':
case 'text':
if($repeatNext && strlen($item['code']) > 1 && !(strlen($item['code']) == 2 && $item['code'][1] == '\\')){
return '(?:' . $item['code'] . ')';
}else{
return $item['code'];
}
break;
case 'any':
return ".";
break;
case 'end':
return '$';
break;
case 'start':
return '^';
break;
case 'repeat':
return $item['code'];
break;
}
}
}
class RegCode {
public $flags;
public $chain;
public function __construct($flags = ""){
if(is_string($flags)){
$this->flags = $flags?:"";
}else{
$this->flags = $flags->flags;
$this->chain = clone $flags->chain;
}
}
public function start(){
$this->chain[] = array('type'=>"start");
return $this;
}
public function end(){
$this->chain[] = array('type'=>"end");
return $this;
}
public function range($allowed, $escape = true){
if($escape) $allowed = RegCodeFn::escape($allowed);
$this->chain[] = array(
'type'=>'range',
'not'=>false,
"allowed"=>$allowed
);
return $this;
}
public function notRange($allowed, $escape = true){
if($escape == null) $escape = true;
if($escape) $allowed = RegCodeFn::escape($allowed);
$this->chain[] = array(
'type'=>'range',
'not'=>true,
"allowed"=>$allowed
);
return $this;
}
public function group($name, $assertion = null, $chain = null){
$chain = $chain == null?($assertion == null ? $name : $assertion):$chain;
$assertion = is_string($assertion) ? $assertion : false;
$name = is_string($name) ? $name : false;
$this->chain[] = array(
'type'=>'group',
'assertion'=>$assertion,
'name'=>$name,
'chain'=>$chain->chain
);
return $this;
}
public function any(){
$this->chain[] = array("type"=>'any');
return $this;
}
public function repeat($min, $max = false){
$code = ($min == '?' && $min == '*' && $min == '+')?$min:'{'.($min?:0) . ($max===false?',' . ($max?:''):'') . '}';
$this->chain[] = array(
"type"=>"repeat",
"code"=>$code
);
return $this;
}
public function raw($code){
$this->chain[] = array(
"type"=>'raw',
"code"=>$code
);
return $this;
}
public function text($text,$escape){
if($escape == null) $escape = true;
$this->chain[] = array(
'type'=>'text',
"code"=>$escape?RegCodeFn::escape($text):$text
);
return $this;
}
public function __toString(){
return "/" . RegCodeFn::compileArr($this->chain) . "/" . $this->flags
}
public function compile($force){
return new RegCodeCompiled(RegCodeFn::compileArr($this->chain), $this->flags);
}
}
class RegCodeCompiled {
public $code;
public $flags;
public function __construct($code, $flags){
$this->code = $code;
$this->flags = $flags;
}
public function match($subject, &$matches = null, $flags = 0, $offset = 0){
return preg_match("/" . $this->code . "/" . $this->flags, $subject, $matches, $flags, $offset);
}
public function match_all(){
}
public function replace($replacement, $subject, $limit = -1, &$count = null){
return preg_replace("/" . $this->code . "/" . $this->flags, $replacement, $subject, $limit, &$count)
}
public function replace_callback($callback, $subject, $limit = -1, &$count = null){
return preg_replace_callback("/" . $this->code . "/" . $this->flags, $callback, $subject, $limit, &$count)
}
}
echo RC()
->start()
->range('a-z')
->range('a-z0-9')->repeat(3,null)
->end()
- Output for 5.4.0 - 5.4.26
- Parse error: syntax error, unexpected 'class' (T_CLASS), expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in /in/6sDkX on line 6
Process exited with code 255. - Output for 5.3.0 - 5.3.28
- Parse error: syntax error, unexpected T_CLASS, expecting T_PAAMAYIM_NEKUDOTAYIM in /in/6sDkX on line 6
Process exited with code 255.
preferences:
197.85 ms | 1395 KiB | 63 Q