PHP REGEX preg_replace_callback not matching

I am trying to match

[history="e70b3ffc-beaf-423f-b084-72bc5bae3147"]History name here[/history]

and get the ID and the content between the "tags". And although the above works fine at https://regexr.com/3ilt2 it's won't match on my code and I cannot figure out why. Any ideas?

    $parsed_string = preg_replace_callback(
        // [history="ID"]ABC[/history]
        '/\[history="(.*?)"\](.*?)\[\/history\]/', 
        function ($matches) {
            return $this->parseHistories( $matches, 'alt-name' );
        }, 
        $parsed_string
    );

Upvotes: 0

Views: 52

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521429

The preg_replace_callback function works fine in the following code:

$parsed_string = "[history=\"e70b3ffc-beaf-423f-b084-72bc5bae3147\"]History name here[/history]";
$parsed_string = preg_replace_callback(
    // [history="ID"]ABC[/history]
    '/\[history="(.*?)"\](.*?)\[\/history\]/', 
    function ($matches) {
        return $matches[1];    // return the first capture group (the UUID)
    }, 
    $parsed_string
);
echo $parsed_string;

This outputs the captured UUID e70b3ffc-beaf-423f-b084-72bc5bae3147. There must be a problem with your parseHistories() function.

Demo

Upvotes: 1

Related Questions