1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Coding help - [PHP - Preg]

Discussion in 'Off Topic' started by James Freeman, Dec 14, 2010.

  1. James Freeman

    James Freeman Member

    I'm currently working on a template system like xenForo's, I've got my code working partly, I'm kinda new to the whole preg_match and preg_replace..

    Right, I'll show you some code..

    test.tpl:
    Code:
    <curve:if condition="test">
        Hai
        <curve:else>
            had
        </curve:else>
    </curve:if>
    
    However it's being translated to..
    Code:
    <curve:if condition="test">
    
    
    
        Hai
    
    
         <?php } else { ?>
    
    
            had
    
    
         <?php } else { ?>
    
    
    </curve:if>
    
    My PHP code is as follows:

    PHP:
    <?php
            
    private function _handleElse ( )
            {
                
    $find '#(<curve:if)(.*)(<curve:else>|</curve:else>)(.*)</curve:if>#is';
                
    $input $this->_outPut;
                while( ( bool ) 
    preg_match$find$input ) )
                {
                    
    $input preg_replace
                    
    (
                        
    $find,
                        
    '<curve:if$2 <?php } else { ?> $4</curve:if>',
                        
    $input
                    
    );
                }
                echo 
    $input;die;
            }
     
  2. Jaxel

    Jaxel Well-Known Member

    I dont know exactly what you're trying to do... but it appears you don't have a lot of understanding of Regular Expressions. In this case, I recommend you DON'T use them. Without a deep knowledge of regular expressions, they are very dangerous. In your code above, you have already made a major mishap with your use of greedy catch alls.
    Not to mention, you won't be able to do nested conditions unless you program each one individually.
     
    OSS 117 likes this.

Share This Page