3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php echo sqlTemplate("{{o}} {[a]} {{u:BLABLABLA {{a}} }}",array("o"=>"HELLO","a"=>array(1,323,452),"u"=>false)); function sqlTemplate($query,$vars){ /* $replace = function($matches) use ($vars) { $varName = trim($matches[2]); if(count($matches)==5){ return isset($vars[$varName])&&$$vars[$varName]?$this->sqlTemplate($matches[4],$vars):""; }else if($matches[1] == '['){ return implode(',',$vars[$varName]); }else if($matches[1] == '{'){ return $vars[$varName]; }else{ return $matches[0]; } };*/ $i = 0;$l = strlen($query)-4; $bigRecord = ""; while($i < $l){ var_dump($query[$i]); if($query[$i] == '{' && ($query[$i+1] == '{' || $query[$i+1] == '[')){ $i+=2; $end = false; $level=0; $record = $query[$i+1]; while($i < $l){ $record .= $query[$i]; echo $record; if($query[$i] == '{' && ($query[$i+1] == '{' || $query[$i+1] == '[')) $level++; if(($query[$i] == ']' && $query[$i] == '}') || $query[$i+1] == '}') $level--; if($level==-1){ echo $record; if($record[0] == '['){ $bigRecord .= impode(',',$vars[trim(substr($record,1,-1))]); }else{ $pos = false; if($pos = strpos($record, ':')){ $varName = trim(substr($record,1,$pos-1)); if(isset($vars[$varName]) && $vars[$varName]){ $bigRecord .= $sqlTemplate(substr($record,$pos,-1)); } }else{ $bigRecord .= $vars[trim(substr($record,1,-1))]; } } break; } $i++; } }else{ $bigRecord .= $query[$i]; } $i++; } return $bigRecord; //return preg_replace_callback('~\{([\[\{])([a-z0-9\_\-]+)(\:([^\}]*))?[\]\}]\}~i',$replace, $query); }
Output for 5.4.0 - 7.1.0
string(1) "{" }o}o Notice: Undefined index: in /in/0T6is on line 44 string(1) "}" string(1) "}" string(1) " " string(1) "{" ]a]a]]a] Notice: Array to string conversion in /in/0T6is on line 44 string(1) "}" string(1) " " string(1) "{" :u:u::u:B:u:BL:u:BLA:u:BLAB:u:BLABL:u:BLABLA:u:BLABLAB:u:BLABLABL:u:BLABLABLA:u:BLABLABLA :u:BLABLABLA {:u:BLABLABLA {{:u:BLABLABLA {{a:u:BLABLABLA {{a}:u:BLABLABLA {{a} Notice: Undefined index: u:BLABLABLA {{a in /in/0T6is on line 44 }} Array}
Output for 5.3.0 - 5.3.29
string(1) "{" }o}o Notice: Undefined index: in /in/0T6is on line 44 string(1) "}" string(1) "}" string(1) " " string(1) "{" ]a]a]]a]string(1) "}" string(1) " " string(1) "{" :u:u::u:B:u:BL:u:BLA:u:BLAB:u:BLABL:u:BLABLA:u:BLABLAB:u:BLABLABL:u:BLABLABLA:u:BLABLABLA :u:BLABLABLA {:u:BLABLABLA {{:u:BLABLABLA {{a:u:BLABLABLA {{a}:u:BLABLABLA {{a} Notice: Undefined index: u:BLABLABLA {{a in /in/0T6is on line 44 }} Array}
Output for 4.3.0 - 5.2.17
string(1) "{" }o}o Notice: Undefined index: in /in/0T6is on line 44 string(1) "}" string(1) "}" string(1) " " string(1) "{" ]a]a]]a]string(1) "}" string(1) " " string(1) "{" :u:u::u:B:u:BL:u:BLA:u:BLAB:u:BLABL:u:BLABLA:u:BLABLAB:u:BLABLABL:u:BLABLABLA:u:BLABLABLA :u:BLABLABLA {:u:BLABLABLA {{:u:BLABLABLA {{a:u:BLABLABLA {{a}:u:BLABLABLA {{a} Notice: Undefined index: u:BLABLABLA {{a in /in/0T6is on line 44 }} Array}