note 52461 deleted from function.preg-replace-callback by colder

From: Date: Mon, 18 Jun 2007 18:45:16 +0000
Subject: note 52461 deleted from function.preg-replace-callback by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-127654@lists.php.net to get a copy of this message
Note Submitter: larry at fantasyclub dot ru ---- A powerful php template parser may be written in one line of code :) (Of course there are many ways of it, this is my own just developed) print preg_replace_callback("/<\\?(.*?)\\?>/", create_function('$m','extract($GLOBALS); return(eval("return($m[1]);"));'),$s); Template example: <?$var["x1"]?><br> <?$var["x2"]?><br> <?$var["x1"]." php foreva ".$var["x2"]?><br> So, as you can see, <? and ?> are template parser tags and it is even possible to make simple php expressions inside such tags. However not too complex to confuse designer :) You may add \" around inner $m[1] at your taste and templates will be little different ;) Another advantage of such parser that you do not need to worry about variables must be available in templates - all globals and global arrays will be :) Very handy in fact. As I saw above, this can be optimized by making separate (not inline) callback function, and I will of courde do it in real program, but this is less gracefull than I wrote here :)

« previous php.notes (#127654) next »