note 52461 added to function.preg-replace-callback
| From: | larry at fantasyclub dot ru | Date: | Mon, 02 May 2005 16:08:03 +0000 |
| Subject: | note 52461 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88866@lists.php.net to get a copy of this message | ||
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 :)
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52461
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52461&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52461&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52461&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52461&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52461&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52461&report=yes
Search -- http://master.php.net/manage/user-notes.php