note 58016 added to function.get-defined-vars

From: Date: Fri, 21 Oct 2005 16:08:23 +0000
Subject: note 58016 added to function.get-defined-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-97359@lists.php.net to get a copy of this message
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; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.get-defined-vars.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58016 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58016&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58016&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58016&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58016&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58016&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58016&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58016&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58016&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97359) next »