eval - How to create own php templating system? -
i have 2 files name test.txt
contains template code.i need evalute template evaluated php. eval()
function giving errors on ifelse , other similar conditions.
test.txt
@$firmware_path=true; @$dialplan=1312321; @$max_lines=3; @$data=array(); @$operator_ip=''; @$enabled=true; @ if ($firmware_path) @{ firmware server: http://{$operator_ip}{$firmware_path} @ } @ ($i = 1; $i <= $max_lines; $i++) @ { @ $enabled = isset($lines[$i - 1]); @ if ($enabled) @{ @ $data = $lines[$i -1]; @ if ($data['user_fullname']) @{ @ if ($phone_label) @{ @ $screenname = $data['user_fullname'] . ' ' . $data['telnum']; @ $screenname2 = $phone_label; @ } @else @{ @ $screenname = $data['user_fullname']; @ $screenname2 = $data['telnum']; @ } @ } @else @{ @ $screenname = $data['telnum']; @ $screenname2 = $phone_label; @ } @ } @ }
index.php
<?php $file = fopen("test.txt","r"); $arr=array(); while(! feof($file)) { $arr[]=fgets($file); } $format=''; foreach($arr $key=>$value) { if(substr(ltrim($value), 0, 1) === '@') { $result=str_replace('@','',$value); $format.=$result.php_eol; } else { $format.='$final="'.$value.'";'; } } $format.=' return $final;'; echo eval($format); ?>
the result should generated according php
i solved problem . php templating , in try use custom changeable code executed in scenarios.
here algorithm used :
note put template code in
$arr
$arr=str_replace('transliterate','urldecode',$arr); $format='$final="";'; foreach($arr $key=>$value) { if(substr(ltrim($value), 0, 1) === '@') { //run php line through eval $result=str_replace('@','',$value); $format.=$result.php_eol; } else { // echo normal php echo line $value=str_replace('"','\"',$value); $value=str_replace('{','".(',$value); $value=str_replace('}',')."',$value); $format.='$final.="'.$value.'";'.php_eol; } } $format.=' return $final;'; $evaled_result= eval($format);
Comments
Post a Comment