3v4l.org

run code in 300+ PHP versions simultaneously
<?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()

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
5.4.260.0300.07319.13
5.4.250.0270.03319.19
5.4.240.0100.04319.07
5.4.230.0200.07018.83
5.4.220.0100.04719.13
5.4.210.0130.04019.08
5.4.200.0170.04019.08
5.4.190.0170.04318.86
5.4.180.0230.07319.01
5.4.170.0170.04319.18
5.4.160.0130.04318.99
5.4.150.0270.04318.82
5.4.140.0230.06716.26
5.4.130.0200.04016.40
5.4.120.0270.03016.34
5.4.110.0170.06716.56
5.4.100.0200.03716.46
5.4.90.0130.07316.54
5.4.80.0200.07016.26
5.4.70.0200.05716.46
5.4.60.0130.05716.52
5.4.50.0130.04316.48
5.4.40.0170.03716.53
5.4.30.0270.05716.25
5.4.20.0170.04016.51
5.4.10.0200.05716.32
5.4.00.0330.06015.90
5.3.280.0270.04014.52
5.3.270.0300.04314.61
5.3.260.0270.07314.71
5.3.250.0300.06714.72
5.3.240.0300.06714.68
5.3.230.0200.07014.64
5.3.220.0370.05014.57
5.3.210.0200.07314.68
5.3.200.0230.06314.54
5.3.190.0300.06714.47
5.3.180.0330.06314.55
5.3.170.0230.07314.57
5.3.160.0270.06714.58
5.3.150.0200.05714.49
5.3.140.0230.07014.47
5.3.130.0270.05014.66
5.3.120.0270.04314.62
5.3.110.0200.04714.47
5.3.100.0200.07714.12
5.3.90.0170.04313.99
5.3.80.0200.03714.10
5.3.70.0270.06014.13
5.3.60.0130.04013.96
5.3.50.0200.07013.86
5.3.40.0230.04713.94
5.3.30.0230.06013.82
5.3.20.0330.03313.61
5.3.10.0270.05013.71
5.3.00.0230.05713.69
5.2.170.0200.03713.00
5.2.160.0100.03313.00
5.2.150.0130.03313.00
5.2.140.0130.03013.00
5.2.130.0170.02713.00
5.2.120.0170.02313.00
5.2.110.0170.03013.00
5.2.100.0130.02713.00
5.2.90.0170.02313.00
5.2.80.0100.03013.00
5.2.70.0130.02713.00
5.2.60.0170.02313.00
5.2.50.0070.04013.00
5.2.40.0130.02713.00
5.2.30.0070.03013.00
5.2.20.0170.02313.00
5.2.10.0100.02713.00
5.2.00.0100.03713.00
5.1.60.0100.03013.00
5.1.50.0200.02313.00
5.1.40.0170.01713.00
5.1.30.0030.03013.00
5.1.20.0130.02313.00
5.1.10.0130.02313.00
5.1.00.0100.02713.00
5.0.50.0030.02313.00
5.0.40.0030.02313.00
5.0.30.0070.03013.00
5.0.20.0070.02013.00
5.0.10.0100.01313.00
5.0.00.0070.03313.00
4.4.90.0070.01013.00
4.4.80.0000.01713.00
4.4.70.0070.01313.00
4.4.60.0030.01713.00
4.4.50.0030.01713.00
4.4.40.0100.02013.00
4.4.30.0070.01313.00
4.4.20.0030.01713.00
4.4.10.0030.02013.00
4.4.00.0030.02713.00
4.3.110.0000.02013.00
4.3.100.0030.02013.00
4.3.90.0030.01713.00
4.3.80.0000.02713.00
4.3.70.0070.01713.00
4.3.60.0100.01313.00
4.3.50.0070.02013.00
4.3.40.0070.02313.00
4.3.30.0000.01313.00
4.3.20.0000.01713.00
4.3.10.0000.01713.00
4.3.00.0000.02013.00

preferences:
150.71 ms | 1394 KiB | 7 Q