note 23056 modified in function.eval by danbrown

From: Date: Tue, 25 Nov 2008 16:11:55 +0000
Subject: note 23056 modified in function.eval by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146954@lists.php.net to get a copy of this message
Here an improved function of evalHTML discussed above (see brandon@louish.net or nathan vonnahme). I do some bench with a multi-line string contain php code <?echo "hello" ?> : for 1200 eval/replace => proceed 10 times faster for 12 eval/replace => proceed 3 times faster function also support both <? ?> and <?php ?> style... enjoy ! <?php function eval_buffer($string) { ob_start(); eval("$string[2];"); $ret = ob_get_contents(); ob_end_clean(); return $ret; } function eval_html($string) { return preg_replace_callback("/(<\?php|<\?)(.*?)\?>/si", "eval_buffer",$string); } ?> for older php version (< 4.0.5), it also possible to do it with preg_replace but it's a little slower (20 %) : <?php function eval_buffer($string) { ob_start(); eval(stripslashes("$string;")); $string = ob_get_contents(); ob_end_clean(); return $string; } function eval_html($string) { return preg_replace("/(<\?php|<\?)(.*?)\?>/sie", "eval_buffer('\\2')",$string); } ?> --was-- Here an improved function of evalHTML discussed above (see brandon@louish.net or nathan vonnahme). I do some bench with a multi-line string contain php code <?echo "hello" ?> : for 1200 eval/replace => proceed 10 times faster for 12 eval/replace => proceed 3 times faster function also support both <? and <?php style... enjoy ! ** BEGIN ** function eval_buffer($string) { ob_start(); eval("$string[2];"); $ret = ob_get_contents(); ob_end_clean(); return $ret; } function eval_html($string) { return preg_replace_callback("/(<\?php|<\?)(.*?)\?>/si", "eval_buffer",$string); } ** END ** ?> for older php version (< 4.0.5), it also possible to do it with preg_replace but it's a little slower (20 %) : ** BEGIN ** function eval_buffer($string) { ob_start(); eval(stripslashes("$string;")); $string = ob_get_contents(); ob_end_clean(); return $string; } function eval_html($string) { return preg_replace("/(<\?php|<\?)(.*?)\?>/sie", "eval_buffer('\\2')",$string); } ** END ** http://php.net/manual/en/function.eval.php

« previous php.notes (#146954) next »