note 42760 added to function.preg-replace-callback

From: Date: Fri, 28 May 2004 09:10:47 +0000
Subject: note 42760 added to function.preg-replace-callback
Groups: php.notes 
Request: Send a blank email to php-notes+get-70230@lists.php.net to get a copy of this message
This simple code evaluate PHP code stored in string (generally stored in some database back end). This code will look for <php> as starting tag and </php> as ending tag. In this tag can be any PHP code which result will replace the original code. Sample code: <?php $html_data = 'Some html...<br>'; $html_data .= 'some php output...<php>return 2+2;</php><br>'; $html_data .= 'some php include...<php>include ("file.php");</php>'; function myeval($input) { if (preg_match ("/include/i", $input[1])) { ob_start(); eval($input[1]); $buffer = ob_get_clean(); return $buffer; } else return eval($input[1]); } $tag_search = "/\<php>(.*?)\<\/php>/si"; $html_data = preg_replace_callback($tag_search,"myeval",$html_data); echo $html_data; ?> Output: Some html... some php output...4 some php include...[output of file.php] ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42760 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42760&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42760&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#70230) next »