rm249 Posted October 26, 2007 Share Posted October 26, 2007 Hello, I am pretty good in PHP although I am having a bit of trouble with this regular expression. I am working on a template system function to parse the inputed template file as a variable and find and parse any <if> statements. The way it works is that it detects if the variable is defined or not, if it is show the results, else dont. (Its basic right now but I intend to expand more upon it once I get the basics working correctly). The way I have it setup works great when there aren't any <if> statements within another if, but when there are everything goes all foobar. Any tips as to how to get this to work properly with <if> statements within if statements are greatly appreciated. An example of how the IF is in the template file is below: <if="cu_user_name"> Welcome [cu_user_name]<br /> <if="cu_admin"> Click to go to admin<br /> </if> </if> PHP function that parses the <if> statements: // Parse if statememnts function ifParse($handle,$vars) { preg_match_all('#<if=\"(.*)\">(.*)</if>#Us',$handle,$results); if (count($results)>0) { while(list($key,$val) = each($results[1])) { $data = $results[2][$key]; $pattern = '#<if="'.$val.'">(.*)</if>#Us'; if(array_key_exists($val,$this->vars)) { $handle = preg_replace($pattern,"$1",$handle); } else { $handle = preg_replace($pattern,"",$handle); } } return $handle; } else { return false; } } Link to comment https://forums.phpfreaks.com/topic/74934-template-system-statements/ Share on other sites More sharing options...
Lumio Posted November 10, 2007 Share Posted November 10, 2007 I think regex doesn't do it for you alone. You need a parser... Link to comment https://forums.phpfreaks.com/topic/74934-template-system-statements/#findComment-388537 Share on other sites More sharing options...
rm249 Posted November 10, 2007 Author Share Posted November 10, 2007 Ok, I will take a look into doing that. Thanks Link to comment https://forums.phpfreaks.com/topic/74934-template-system-statements/#findComment-388645 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.