I've got a template system where you can insert custom conditions like this:
HTML Code:
<if="$variable == 'value'">do this</if>
I can do that fine, but with nested conditions, the script screws up because the <if="">'s do not match with the correct </if>'s. Example:
Code:
<if="something"> <-- Will match with...
<if="something else">
yeah
</if> <-- this, instead of
</if> <-- this
I tried to solve this by re-calling the function in itself if there is another <if> statement nested within another one, this would evaluate the nested conditions so that no </if>'s remain, then the parent <if> will match up with the correct </if>. It's a little hard to wrap your head around, but if you understand then any help is appreciated

Here's my code,
PHP Code:
//============================================================================
// doConditions
// - execute conditions
//============================================================================
function doConditions($txt)
{
if(strpos($txt, '<if="') === false)
return $txt;
////////////////////////////////////////////////
// Set things up. More of a reference for me :)
////////////////////////////////////////////////
$if['s_start_len'] = strlen('<if="'); // IF start beginning length
$if['s_start_pos'] = false; // IF start beginning pos (<if=")
$if['s_end_len'] = strlen('">'); // IF start end length
$if['s_end_pos'] = false; // IF start end pos (">)
$if['cond_pos'] = false; // Position of starting condition
$if['s_last'] = false; // Last position of whole <if="">
$if['e_len'] = strlen('</if>'); // IF end length
$if['e_pos'] = false; // IF end pos (</if>)
$else['len'] = strlen('<else>'); // ELSE length
$else['pos'] = false; // ELSE pos (<else>)
$else['last'] = false; // Last position of whole <else>
$else['use'] = false; // Use else?
///////////////////////////////////
// Loop thru until nothing is left
///////////////////////////////////
while($if['s_start_pos'] = strpos($txt, '<if="') !== false)
{
////////////////////////////
// Fetch all this iffy info
////////////////////////////
$if['s_end_pos'] = strpos($txt, '">' , $if['s_start_pos']);
$if['s_last'] = $if['s_end_pos'] + $if['s_end_len'];
$if['e_pos'] = strpos($txt, '</if>', $if['s_last']);
$nested = strpos($txt, '<if="', $if['s_end_pos']);
if($nested !== false && $nested < $if['e_pos'])
{
$txt = substr($txt, 0, $if['s_last']) . $this->doConditions(substr($txt, $if['s_last']));
$if['e_pos'] = strpos($txt, '</if>', $if['s_last']);
}
$if['cond_pos'] = $if['s_start_pos'] + $if['s_start_len'] - 1;
//////////////////////
// See about the else
//////////////////////
$else['pos'] = strpos($txt, '<else>', $if['s_last']);
$else['last'] = $else['pos'] + $else['len'];
if($else['pos'] !== false && $else['pos'] < $if['e_pos'])
$else['use'] = true;
////////////////////////////
// Grab certain needed text
////////////////////////////
$__txt_all = '';
$__txt_cond = '';
$__txt_true = '';
$__txt_false = '';
$__txt_replace = '';
// All text for replacement later
$__txt_all = substr($txt, $if['s_start_pos'] - 1, $if['e_pos'] - $if['s_start_pos'] + $if['e_len'] + 1);
// Condition
$__txt_cond = substr($txt, $if['cond_pos'], $if['s_end_pos'] - $if['cond_pos']);
if(empty($__txt_cond))
$__txt_cond = 'true';
// True
if($else['use'])
$__txt_true = substr($txt, $if['s_last'], $else['pos'] - $if['s_last']);
else
$__txt_true = substr($txt, $if['s_last'], $if['e_pos'] - $if['s_last']);
// False
if($else['use'])
$__txt_false = substr($txt, $else['last'], $else['last'] - $if['e_pos']);
// Replacements
extract($this->vars, EXTR_OVERWRITE);
$__olde = error_reporting(E_ERROR);
eval('$__txt_replace = (' . $txt_cond . ' ? $txt_true : $txt_false);');
error_reporting($__olde);
/////////////////
// Lets dooo it!
/////////////////
$txt = str_replace($__txt_all, $__txt_replace, $__txt);
}
return $txt;
}
(btw, using preg_replace's would be no different

I was going to use regx techniques but there are a few advantages to using this way)