note 30491 deleted from function.preg-replace by sniper
| From: | sniper@php.net | Date: | Sun, 07 Sep 2003 22:02:08 +0000 |
| Subject: | note 30491 deleted from function.preg-replace by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-55686@lists.php.net to get a copy of this message | ||
Note Submitter: dalex3@hotmail.com
----
Here is a perfect function for templates,
it will replace almost any valid variables
like $var / $$var / {$var} / $array[name][0]
Note: Don't put any quotes in key names of array
( $array['name'] - will not work, use $array[name] ).
<?
# for variables inside a function:
$vars_str = preg_replace ("/({)?([$]{1,3}[\w]+(\[[\w]+\])*)(?(1)})/e", "\\2",
$vars_str);
# Or use the function that will work with global variables:
function vars_replace($vars_str) {
extract($GLOBALS, EXTR_REFS); //get global vars
return preg_replace ("/({)?([$]{1,3}[\w]+(\[[\w]+\])*)(?(1)})/e", "\\2",
$vars_str);
}
#example
$test="Your string";
echo vars_replace("This is $test");
?>