note 58019 added to ref.strings

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

« previous php.notes (#97364) next »