Search code examples
phppreg-replace-callback

preg_replace_callback pattern not containing certain sub-string


Currently i am using the following code to modify content between {| and |} using a custom function parse_table

$output = preg_replace_callback("({\|(.*?)\|})is", function($m) {return parse_table($m[1]);}, $input);

Now i want to modify it such that the pattern can exclude a certain substring, such as abcde. What could be done to achieve this?

Thank you very much.


Solution

  • You can put it in your regex:

    "({\|(?!abcde)(.*?)\|})is"
    

    However, this would get very difficult to read, very fast. Instead, use the callback:

    function($m) {
        if( $m[1] == "abcde") return $m[0];
        return parse_table($m[1]);
    }