Jump to content

HTML to PHP logic


-Mike-

Recommended Posts

In a script I'm writing there will be a template engine that will parse <if=""> statements inside of the template file instead of using raw php. It is working so far, but when I put an <if> inside another <if>, when the regex statement searches for the </endif> tag, it grabs the one of the child <if> instead of the parent <if>. Below is the code I'm working with, please it would be really wonderful if someone could help.

					preg_match_all("#<if=\"(.*)\">(.*)(</endif>|<else>(.*)</endif>)#Us", $html2parse, $if_matches);					foreach ($if_matches['0'] as $match_key => $match_value) {						eval('if ('.$if_matches['1'][$match_key].') {							$html2parse = str_replace($if_matches[\'0\'][$match_key], $if_matches[\'2\'][$match_key], $html2parse);						} else {							if (preg_match("#<else>#Us", $if_matches[\'0\'][$match_key])) {								$html2parse = str_replace($if_matches[\'0\'][$match_key], $if_matches[\'4\'][$match_key], $html2parse);							} else {								$html2parse = str_replace($if_matches[\'0\'][$match_key], NULL, $html2parse);							}						}');					}

Link to comment
Share on other sites

I've got it to work, and I'm so stupid that I didn't think of doing it this way.

					$html2parse = preg_replace("#<if=\"(.*)\">#", '<?php if ($1) { ?>', $html2parse);					$html2parse = preg_replace("#<else>#", '<?php } else { ?>', $html2parse);					$html2parse = preg_replace("#</endif>#", '<?php } ?>', $html2parse);					$html2parse = eval('?>'.$html2parse);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...