This example lets you parse an unparsed strings variables. Warning: This could cause security leaks if you allow users to pass $variables through this engine. I recommend only using this for your Content Management System.
<?
$mytime=time();
$mydog="My Dog Ate My PHP!";
# Your Parsing String:
$s1 = 'Hyphen Variable Preserving: $mytime, and $mydog';
echo "Before: <br><br>$s1<br><br>";
# Remember, wherever you define this, it will not be defined GLOBAL into the function
# which is why we define it here. Defining it global could lead to security issues.
$vardata=get_defined_vars();
# Parse the string
$s1 = StrParse($s1,$vardata);
echo "After: <br><br>$s1";
function StrParse($str,$vardata) {
# Takes a string, or piece of data, that contains PHP Variables
# For example, unparsed variables like: Test using time: $mytime
# This example shows $mytime, and not the actual variable value.
# The end result shows the actual variable value of $mytime.
# This is useful for building a content management system,
# and directing your variables into your content data,
# where content is stored in a file or database, unparsed.
# Of course this could slow down page loads, but it's a good way
# to parse data from current variables into your loaded new data
# making it compatible.
# Then the variables are replaced with the actual variable..
$getvarkeys=array_keys($vardata);
$ret=$str;
for ($x=0; $x < count($getvarkeys); $x++) {
$myvar=$getvarkeys[$x];
#echo "Variable: " . $myvar . " [" . $vardata[$myvar] . "]<br>";
$ret=str_replace('$' . $myvar, $vardata[$myvar], $ret);
}
return $ret;
}
?>