Rekursiver RegExp

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rekursiver RegExp

    Hallo zusammen,

    ich schreibe gerade aus Interesse eine "Template Engine".

    Nur habe ich gerade ein Problem mit, ich tippe mal den Regular Expressions, der Rekursiven (Verschachtelten) Überprüfung der If-Conditions, diese greifen mehr oder weniger gar nicht.

    Anbei mal die essentiel wichtigen Methoden für diese Aktion.

    PHP Code:
    protected function parseIfConditions()
    {
        
    $sPattern '#if \(\"(.*?)\" (==|!=|<|>|<=|>={1,2}) \"(.*?)\"\) {(.*?)([^\\\])}#is';

        return 
    preg_replace_callback($sPattern, array($this'parseIfCallback'), $this->sComputedTemplate);
    }
        

    protected function 
    parseIfCallback($aResults)
    {
        if (
    $this->ConditionHelper($aResults)) {
            
    $this->sComputedTemplate str_replace($aResults[0], $aResults[4], $this->sComputedTemplate);
        } else {
            
    $this->sComputedTemplate str_replace($aResults[0], ''$this->sComputedTemplate);
        }


    Die Methode ConditionHelper ist eigentlich nur für folgendes zuständig:

    PHP Code:
    protected function ConditionHelper($aResults)
    {
        if (
    $aResults[2] == "==")
            
    $bResult = ($aResults[1] == $aResults[3] ? true false);
                
        if (
    $aResults[2] == "!=")
            
    $bResult = ($aResults[1] != $aResults[3] ? true false);
                
        if (
    $aResults[2] == ">=")
            
    $bResult = ($aResults[1] >= $aResults[3] ? true false);
                
        if (
    $aResults[2] == "<=")
            
    $bResult = ($aResults[1] <= $aResults[3] ? true false);
                
        if (
    $aResults[2] == ">")
            
    $bResult = ($aResults[1] > $aResults[3] ? true false);
                
        if (
    $aResults[2] == "<")
            
    $bResult = ($aResults[1] < $aResults[3] ? true false);
                
        return 
    $bResult;


    Wenn ich die If-Conditions normal schreibe, funktioniert das ganze.

    Gibs da evtl. einen Trick?

    LG Toby
    Last edited by Skaschy; 09-08-2008, 23:17.

  • #2
    Hallo,

    habs mal quick and dirty so gelöst.


    PHP Code:
    /**
     * Parst die If(-Else) Conditions (verschachtelung möglich)
     * 
     * @example 
     * 
     *         Syntax:
     * 
     *         {if "{foo}" == "bar"}
     *             FooBar
     *         {/if}
     *
     *         {if "{foo}" == "bar"}
     *             bar
     *         {else}
     *             foo
     *         {/if}
     *
     * @param    string      $sTemplate      Template String
     * @return     string
     * @access     protected
     * @author     Tobias Ehrig <toby@armed-assault.eu>
     */
    protected function parseIfConditions($sTemplate)
    {
        
    $sIfCondition            '#{if \"(.*?)\" (==|!=|<|>|<=|>={1,2}) \"(.*?)\"}#is';
        
    $sIfStart                '#{if#is';
        
    $sIfEnd                    '#{/if}#is';
        
    $sElseOperator            '#{else}#is';
        
    $bConditionIsTrue        false;    
        
    $iIteralLevel            0;        
        
    $aTemplateLines            explode("\n"$sTemplate);        
        
        for (
    $i 0$i count($aTemplateLines); $i++) {            
            
            if (
    preg_match($sIfStart$aTemplateLines[$i])) {
                
                
    preg_match($sIfCondition$aTemplateLines[$i], $aResult);
                
                if (
    $this->ConditionHelper($aResult)) {
                    
    $bConditionIsTrue    true;
                } else {
                    
    $bConditionIsTrue    false;
                    
    $bIgnoreContent     true;
                    
    $iConditionIteral    $iIteralLevel;
                }
                
                
    $bInsideCondition    true;
                
    $iIteralLevel++;                
                
            }    
                    
            if (
    preg_match($sElseOperator$aTemplateLines[$i])) {
                
    $bElseCondition        true;
            }
            
            if (
    preg_match($sIfEnd$aTemplateLines[$i])) {
                
                
    $bInsideCondition    false;
                
    $bConditionIsTrue    false;
                
    $bElseCondition        false;
                
    $iIteralLevel--;

                if (
    $iIteralLevel == $iConditionIteral) {
                    
    $bIgnoreContent false;
                }
            }
            
            if (
    $bInsideCondition && $bConditionIsTrue && !$bElseCondition) {
                
                if (!
    preg_match($sIfCondition$aTemplateLines[$i]) && !$bIgnoreContent) {
                    
    $sNewTemplate .= $aTemplateLines[$i];
                }
                
            }
            
            if (
    $bInsideCondition && ($bElseCondition && !$bConditionIsTrue)) {
                
                if (!
    preg_match($sElseOperator$aTemplateLines[$i])) {
                    
    $sNewTemplate .= $aTemplateLines[$i];
                }
                
            }
            
            if (!
    $bInsideCondition) {
                
    $sNewTemplate .= preg_replace($sIfEnd''$aTemplateLines[$i]);
            }
        }    
        return 
    $sNewTemplate;


    Gibts es das auch in "schöner"? :-D
    Last edited by Skaschy; 10-08-2008, 21:34.

    Comment

    Working...
    X