preg_replace - replace string with php-code

Discussion in 'PHP' started by PoPSiCLe, Sep 20, 2013.

  1. #1
    Hi.

    I have a parser-function, which takes BBCode-ish [bracket]-code, and replaces these with the desired content. This works fine, as long as I replace the BBCode string with say HTML or something similar.

    What I want, though, is for a certain replacement to contain a PHP if-function - and this I can't get to work. I was looking at the preg_replace manual, but couldn't really find anything regarding this.

    Does anyone know if it's possible? Currently, I have something similar to this (this is stripped down to just the lines in question):

    function parseDBText($value, $userrole_name) {
        // code replacements
        $value = preg_replace('#\[urlintern\=(.+)\](.+)\[\/urlintern\]#iUs', '<a href="index.php?page=$1">$2</a>', $value);
        $value = preg_replace('#\[role\=(.+)\](.+)\[\/role\]#iUs', "<?php if ($userrole_name == 'Superadmin') { echo $userrole_name; ?>$2<?php } ?>", $value);
            return($value);
    }
    PHP:
     
    Solved! View solution.
    PoPSiCLe, Sep 20, 2013 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Just a quick update - the replacement code in the above example is the one that doesn't work - no errors or anything, it just doesn't work - it removes the [role]-bbcode, but does not trigger the if-statement - if I replace the php-code in the replacement with say, a <span class="role"> it replaces it perfectly. And the [role] is surrounding the [urlintern]-code, so it's meant as a way to hide certain internal urls from view for certain users.
     
    PoPSiCLe, Sep 20, 2013 IP
  3. #3
    In PHP you can not place control structures (if, switch, loop, etc..) as params. You would need to do the if statement beforehand and construct the $replacement as a string then pass it to preg_replace, ex:

    
    <?php
    if ($userrole_name == 'Superadmin') {
        $replacement = $userrole_name . '$2';
    } else {
        $replacement = '$2';
    }
    
    $value = preg_replace('#\[role\=(.+)\](.+)\[\/role\]#iUs', $replacement, $value);
    
    Code (markup):
     
    ThePHPMaster, Sep 20, 2013 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Okay. I was afraid it was something like that. That means I'll have to rethink a few things when it comes to the BBCode, but elsewise it'll be fine. Thanks for the help!
     
    PoPSiCLe, Sep 20, 2013 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    You can execute functions or a single expression on preg_replace using the PREG_REPLACE_EVAL modifier (e), ex:

    
    <?php
    $value = '[role=test]test2 [/role]';
    $value = preg_replace('#\[role\=(.+)\](.+)\[\/role\]#iUse', "trim('$2')", $value);
    echo $value;
    
    PHP:
    Additionally, you can use your custom function using http://php.net/manual/en/function.preg-replace-callback.php method.
     
    ThePHPMaster, Sep 20, 2013 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    I ended up doing the following, within my function:

    
      $value = preg_replace_callback('#\[role\=(.+)\](.+)\[\/role\]#iUs', function ($matches) use ($userrole) { if ($matches[1] >= $userrole) { return $matches[2]; }; }, $value);
     
    PHP:
     
    PoPSiCLe, Sep 20, 2013 IP