note 29091 added to ref.strings
| From: | webmaster at gamequbed dot com | Date: | Sat, 01 Feb 2003 21:14:40 +0000 |
| Subject: | note 29091 added to ref.strings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-43276@lists.php.net to get a copy of this message | ||
I was recently working on a template engine, and i found that i wanted to add parameters to my
template includes, for example,
{SUBJECTFIELD template="LINK_ROW" class="subjectfield"}
I found that this code worked out quite well! It converts the string into
$fileParam["SUBJECTFIELD"]["template"]
which will print
"LINK_ROW". It also works for multiple blocks. Have fun!
$text = "
{SUBJECTFIELD class=\"alternate\" rowtemplate=\"LINK_ROW\"}
{BODYFIELD class=\"alternate2\" rowtemplate=\"LINK_ROW\"}";
## remove quotes
$text = str_replace("\"","",$text);
## set counter
$counter = 0;
for ($i=0;$i<strlen($text);$i++)
{
## convert each {...} block into an array element
if ($text[$i] == "{")
{
$currentEndBrace = strpos($text,"}");
$param[$counter] = substr($text,$i,$currentEndBrace);
## remove the end brace
$text[$currentEndBrace] = "";
$counter++;
}
}
$fileParam = array();
for ($j=0;$j<count($param);$j++)
{
## remove trailing stuff
$param[$j] = rtrim($param[$j]);
## and the braces
$param[$j] = ereg_replace("{","",$param[$j]);
$param[$j] = ereg_replace("}","",$param[$j]);
## get the name of the template
$tempIdent = substr($param[$j],0,strpos($param[$j]," "));
## get the rest of the vars
$param[$j] = substr($param[$j],strpos($param[$j]," ")+1);
## make a new array
$fileParam[$tempIdent] = $param[$j];
}
while(list($key,$val) = each($fileParam))
{
## set up as a query string
$val = str_replace(" ","&",$val);
## parse it to a new array
parse_str($val,$eachVal);
## and assign it to another array
$fileParam[$key] = $eachVal;
}
--
http://www.php.net/manual/en/ref.strings.php
http://master.php.net/manage/user-notes.php?action=edit+29091
http://master.php.net/manage/user-notes.php?action=delete+29091
http://master.php.net/manage/user-notes.php?action=reject+29091