note 30491 added to function.preg-replace
| From: | dalex3 at hotmail dot com | Date: | Wed, 19 Mar 2003 23:44:05 +0000 |
| Subject: | note 30491 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-45531@lists.php.net to get a copy of this message | ||
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");
?>
--
http://www.php.net/manual/en/function.preg-replace.php
http://master.php.net/manage/user-notes.php?action=edit+30491
http://master.php.net/manage/user-notes.php?action=delete+30491
http://master.php.net/manage/user-notes.php?action=reject+30491