note 58016 deleted from function.get-defined-vars by betz

From: Date: Fri, 21 Oct 2005 16:18:00 +0000
Subject: note 58016 deleted from function.get-defined-vars by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-97363@lists.php.net to get a copy of this message
Note Submitter: admin at fivestarbuy dot com ---- This example shows you how to use your PHP variables in Content Management Systems you may create with MySQL, or Storing them as files. This makes it a little bit easier instead of adding the PHP syntax everywhere, but it slows things down. This is going to be used on my Content Management system in parts. For example, I will take a string: $s1=' My dogs name: $mydog He ate at: $mytime '; We use Hyphens to preserve the variables. You may want to include a Hyphen replacer to fix hyphens added to the string. Most likely this block $s1 would come from a Web Form, so some tweaking would be needed. Now using my parsing engine, (this is the only way I know how to do it), you can parse the $mydog and $mytime by calling a function. See below. I was hoping PHP had a built in function, but i spent less time just making one, than looking for one. PLEASE NOTE I have not tested this with Sessions yet. So feel free to tweak it up ;) <? $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; } ?>

« previous php.notes (#97363) next »