note 83074 added to language.variables
| From: | maxmike at gmail dot com | Date: | Thu, 08 May 2008 20:15:48 +0000 |
| Subject: | note 83074 added to language.variables | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139193@lists.php.net to get a copy of this message | ||
I'm certain there is an easier way to do this, but I figured I would post it just in-case.
Basically if you have a string that is already set up (loaded from a database etc) and it has php
variables within it, I do not know of a way to have php sub in the values directly.
I've written a couple functions that work around this. I use it in my templating engine which
has to parse files and sub variables. If you use these I simply ask that my email, handle, and name
remain somewhere in a comment in the php file (doesn't have to show up anywhere on the page
obviously, just keep the comment with the code please):
<?php
/***********************************************\
| getVariable and parseStringVariables |
| Originally created by M2tM (Michael Hamilton) |
| www.mutedvision.net maxmike@gmail.com |
\***********************************************/
//Grabs the value of a variable in the global scope from a string
//Also supports 1, 2, and 3 dimensional arrays (not gracefully implemented, but it works)
function getVariable($VariableString){
$variable = str_replace("$", "", $VariableString);
$variable2 = str_replace("]", "", $variable);
$variable2 = str_replace("'", "", $variable2);
$variable2 = str_replace("\"", "", $variable2);
$varArray = explode("[", $variable2);
$compArray[0] = substr($compArray[0], 1, strlen($compArray[0])-1);
$returnVar = "[VARIABLE_DOES_NOT_EXIST]";
if(sizeof($varArray)==1){
global ${$variable};
if(isset(${$variable})){
$returnVar = ${$variable};
}else{
$returnVar = "[VARIABLE_DOES_NOT_EXIST]";
}
}else if(sizeof($varArray)==2){
global ${$varArray[0]};
if(isset (${$varArray[0]}[$varArray[1]])){
$returnVar = (${$varArray[0]}[$varArray[1]]);
}else{
$returnVar = "[VARIABLE_DOES_NOT_EXIST]";
}
}else if(sizeof($varArray)==3){
global ${$varArray[0]};
if(isset (${$varArray[0]}[$varArray[1]][$varArray[2]])){
$returnVar = (${$varArray[0]}[$varArray[1]][$varArray[2]]);
}else{
$returnVar = "[VARIABLE_DOES_NOT_EXIST]";
}
}else if(sizeof($varArray)==4){
global ${$varArray[0]};
if(isset (${$varArray[0]}[$varArray[1]][$varArray[2]][$varArray[3]])){
$returnVar = (${$varArray[0]}[$varArray[1]][$varArray[2]][$varArray[3]]);
}else{
$returnVar = "[VARIABLE_DOES_NOT_EXIST]";
}
}
return $returnVar;
}
//returns a string with the contents of all php variables within a string
//Arrays should be referenced as: $array[val] or $array[0]
//do not use quotations inside the brackets in the string
function parseStringVariables($String){
$totalChars = strlen($String);
$returnString = "";
for($i = 0;$i < $totalChars;$i++){
if($String[$i] === '$'){
$i++;
$VariableString = "";
while($i < $totalChars && (ord($String[$i]) >= 48 &&
ord($String[$i]) <= 57) || (ord($String[$i]) >= 65 && ord($String[$i]) <= 90) ||
(ord($String[$i]) >= 97 && ord($String[$i]) <= 122) || $String[$i] === '['
|| $String[$i] === ']' || $String[$i] === "_"){
$VariableString.=$String[$i];
$i++;
}
$Output = $VariableString;
$VariableValue = getVariable($VariableString);
if($VariableValue === "[VARIABLE_DOES_NOT_EXIST]"){
$returnString.="$".$Output;
}else{
$returnString.=$VariableValue;
}
$i--;
}else{
$returnString.=$String[$i];
}
}
return $returnString;
}
?>
----
Server IP: 216.235.15.211
Probable Submitter: 24.72.38.73
----
Manual Page -- http://www.php.net/manual/en/language.variables.php
Edit -- https://master.php.net/note/edit/83074
Del: integrated -- https://master.php.net/note/delete/83074/integrated
Del: useless -- https://master.php.net/note/delete/83074/useless
Del: bad code -- https://master.php.net/note/delete/83074/bad+code
Del: spam -- https://master.php.net/note/delete/83074/spam
Del: non-english -- https://master.php.net/note/delete/83074/non-english
Del: in docs -- https://master.php.net/note/delete/83074/in+docs
Del: other reasons-- https://master.php.net/note/delete/83074
Reject -- https://master.php.net/note/reject/83074
Search -- https://master.php.net/manage/user-notes.php