<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
	$str = "<p>euiesnutisnt [IF] [ADDRESS] [EQ] thdf [ECHO] nthnth [ADDRESS] [ENDIF] thnthnthnthnth gtgtgtg</p>";
	$str .= "<p>tsnto sntosentusntsn sntsnt [IF] [CITY] [NE] [ECHO] nthnth [CITY] [ENDIF] thnthnt hnthnth ntlszzz</p>";
	$_POST['user_defined_placeholders'] = "[ADDRESS]:address,[CITY]:city";
	$_POST['address'] = "123 Main St.";
	$_POST['city'] = "Cleveland";
	
	$user_defined_placeholders_str = $_POST['user_defined_placeholders'];
	$user_defined_placeholders_arr = explode(",", $user_defined_placeholders_str);
	$user_defined_placeholders = array();
	foreach ($user_defined_placeholders_arr as $placeholder_pair)
	{
		$placeholder_pair_arr = explode(":", $placeholder_pair);
		$user_defined_placeholders[$placeholder_pair_arr[0]] = $placeholder_pair_arr[1];
	}
	
	
	// Process HTML message.
	$msg_arr = explode("[IF]", $str);
	if (count($msg_arr) > 1)
	{
		$msg = $msg_arr[0];
		$if_cnt = count($msg_arr) - 1;
		for ($i = 0; $i < $if_cnt; $i++)
		{
			echo "i: " . $i . "<br>\n\n";
			if ($i > 0)
			{
				$start_msg_str = explode("[ENDIF]", $msg_arr[$i]);
				$msg .= $start_msg_str[1];
				echo "<br><br>\n\n start_msg_str[1]: " . $start_msg_str[1];
			}
			$if_arr = explode("[ENDIF]", $msg_arr[$i + 1]);
//			echo "<pre>\n";
//			print_r($if_arr);
//			echo "</pre>\n";
			$if_str = $if_arr[0];
			if (strpos($if_str, "[EQ]") !== false)
			{
				$comp_arr = explode("[EQ]", $if_str);
				$echo_arr = explode("[ECHO]", $comp_arr[1]);
				$post_var_placeholder = trim($comp_arr[0]);
				$post_var_key = $user_defined_placeholders[$post_var_placeholder];
				if ($_POST[$post_var_key] == trim($echo_arr[0]))
					$msg .= str_replace($post_var_placeholder, $_POST[$post_var_key], $echo_arr[1]);
				echo "<br><br>\n\n echo_arr[1]: " . $echo_arr[1];
			}
			elseif (strpos($if_str, "[NE]") !== false)
			{
				$comp_arr = explode("[NE]", $if_str);
				$echo_arr = explode("[ECHO]", $comp_arr[1]);
				$post_var_placeholder = trim($comp_arr[0]);
				$post_var_key = $user_defined_placeholders[$post_var_placeholder];
				echo "<br><br>post_var_key: $post_var_key\n";
				echo "<br><br>\n\n POST[post_var_key]: " . $_POST[$post_var_key] . " -- echo_arr[0]" . trim($echo_arr[0]) . "<br><br>\n\n";
				if ($_POST[$post_var_key] != trim($echo_arr[0]))
					$msg .= str_replace($post_var_placeholder, $_POST[$post_var_key], $echo_arr[1]);
				echo "<br><br>\n\n echo_arr[1]: " . $echo_arr[1];
			}
			elseif (strpos($if_str, "[GT]") !== false)
			{
				$comp_arr = explode("[GT]", $if_str);
				$echo_arr = explode("[ECHO]", $comp_arr[1]);
				$post_var_placeholder = trim($comp_arr[0]);
				$post_var_key = $user_defined_placeholders[$post_var_placeholder];
				if ($_POST[$post_var_key] > trim($echo_arr[0]))
					$msg .= str_replace($post_var_placeholder, $_POST[$post_var_key], $echo_arr[1]);
			}
			elseif (strpos($if_str, "[LT]") !== false)
			{
				$comp_arr = explode("[LT]", $if_str);
				$echo_arr = explode("[ECHO]", $comp_arr[1]);
				$post_var_placeholder = trim($comp_arr[0]);
				$post_var_key = $user_defined_placeholders[$post_var_placeholder];
				if ($_POST[$post_var_key] < trim($echo_arr[0]))
					$msg .= str_replace($post_var_placeholder, $_POST[$post_var_key], $echo_arr[1]);
			}
//			$msg .= $if_arr[1];
//			echo "\n\n<br><br>if_arr[1]: " . $if_arr[1] . "<br><br>\n\n";
		}
		$end_msg_arr = explode("[ENDIF]", $msg_arr[$if_cnt]);
		$msg .= $end_msg_arr[1];
		echo "end_msg_arr[1]: " . $end_msg_arr[1];
	}
	else
		$msg = $str;
		
	echo "\n\n<br><br><br><br>\n\n";
	echo $msg;
																 
?>
</body>
</html>