SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace() adding back slashes to code?

    I'm writing a template engine, and I'm trying to give full control of functions, classes, etc...

    I'm using this: (not really, but it's the same effect)
    PHP Code:
    preg_replace('!{%FUNCTION\(([^\)]+)\(([^\),?]*)\)\)%}!e''$1($2);'$a); 
    (An example that works would be {%FUNCTION(bcadd(1, 2))%})

    But {%FUNCTION(print('12'))%} doesn't... PHP is inserting backslashes before the quotes...

    I'm getting these errors:
    Code:
    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\Apache2\htdocs\rpg\.classes\template.php(137) : regexp code on line 1
    
    Parse error: parse error, unexpected $end in C:\Apache2\htdocs\rpg\.classes\template.php(137) : regexp code on line 1
    
    Fatal error: preg_replace() [function.preg-replace.php]: Failed evaluating code: print(\'12\'); in C:\Apache2\htdocs\rpg\.classes\template.php on line 137
    Anything???
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Nashville, TN
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try running it through stripslashes() before preg_replace().

  3. #3
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean? stripslashes(\'12\') isn't exactly valid code, either way...
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Nashville, TN
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're trying to parse a variable containing something like {%FUNCTION(print('12'))%} with preg_replace(), right? I'm saying run that variable through stripslashes() before you pass it to preg_replace() (unless I'm misunderstanding the problem).

    It looks like whatever is obtaining the value for the variable containing {%FUNCTION(print('12'))%} is what's responsible for adding the additional slashes, not preg_replace().

  5. #5
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure that it's preg_replace()...

    Here's the code that I'm using right now:
    PHP Code:
    // --snip--
            
    $replace = array (
                
    'str_replace' => array (
                    
    'replace' => array (
                        
    '!{%CONTENT%}!'
                    
    ), 'with' => array (
                            &
    $this->content
                        
    )
                ), 
    'preg_replace' => array (
                    
    'replace' => array (
                        
    '!{%GETPATH\(([^,\)]+),?([^\)]+)\)%}!e',
                        
    '!{%FUNCTION\(([^\)]+)\(([^\)]*)\)\)%}!e'
                    
    ), 'with' => array (
                        
    'getPath(\'$1\', $2)',
                        
    '$1($2);'
                    
    )
                )
            );

            
    // set it
            
    $this->final $this->template;

            
    // temp
            
    echo htmlentities($this->template); exit;

            
    // loop through, and preg/str_replace()
            
    foreach ($replace as $function => $array) {
                
    $temp $array['replace'];
                foreach (
    $temp as $key => $value) {
                    if (!isset(
    $array['with'][$key])) {
                        continue;
                    }

                    
    $this->final $function($value$array['with'][$key], $this->final);
                }
            }
    // --snip-- 
    There's no backslashes in the output. Thanks for your help, though...
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •