Jump to content

HTML to PHP logic


-Mike-
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...