preg_replace Help

Discussion in 'PHP' started by J.T.D., May 9, 2008.

  1. #1
    Ok. I'm trying to include files using a tag "{T.filename}". Here's my code:

    <?php
    $str = '{T.test} test {T.lawl}';
    
    preg_replace("#{T.(.+?)}#is", include(\\1), $str);
    ?>
    PHP:
    I don't know how I would include it successfully. Any help?

    - JTD
     
    J.T.D., May 9, 2008 IP
  2. TeraTask

    TeraTask Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Check out "Example #4 Using the 'e' modifier" on the preg_replace php.net page. That's what allows you to execute functions in preg_replace.
     
    TeraTask, May 9, 2008 IP
  3. J.T.D.

    J.T.D. Peon

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Could you please tell me how? I've been trying:

    
    $str = '{T.tester} hi';
    preg_replace("/{T.(.+?)}/e", "file_get_contents(eval('\\1'))", $str);
    
    PHP:
    
    $str = '{T.tester} hi';
    preg_replace("/{T.(.+?)}/e", "'.include('\\1').'", $str);
    
    PHP:
    
    $str = '{T.tester} hi';
    preg_replace("/{T.(.+?)}/e", include("'\\1'")), $str);
    
    PHP:
    And many more variations. I still don't get it =S
     
    J.T.D., May 10, 2008 IP
  4. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #4
    preg_match("#{T\.([\S]+)}#i","{T.test} test {T.lawl}", $match);
    /*
    $match[1] is test
    */
    
    PHP:
     
    hip_hop_x, May 10, 2008 IP
  5. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #5
    This works on PHP 5:

    
    <?php
    function inc($match){
            include($match[1]);
    }
    
    $str = '{T.test} test {T.lawl}';
    
    echo preg_replace_callback('#{T\.(.+?)}#', inc, $str);
    ?>
    
    PHP:
    Rep would be appreciated ;)

    Jay
     
    jayshah, May 10, 2008 IP
    J.T.D. likes this.
  6. J.T.D.

    J.T.D. Peon

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That only shows test, and doesn't show more stuff, like lawl...

    Thanks +Rep. ;)

    Thanks everyone!!

    - JTD
     
    J.T.D., May 10, 2008 IP