0

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
    );
1
  • 1
    Your code is fine, except for parseHistories which I'm assuming is doing something wrong. Maybe show us that missing code. Commented Jan 2, 2018 at 8:29

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

8 Comments

You have added a \ after the [history= which was not there originally and after the UUID the string I am trying to pass shouldn't have it. are you escaping- I am a bit confused. I can see that the above is working but I cannot understand
@DimitrisRomeoHavlidis I just cut and paste your pattern, that's all.
He's talking about subject string. Yes @DimitrisRomeoHavlidis, it's escaping.
@Revo I'm surprised that his code would even run without escaping a double quote inside a double quoted string.
I know what you are trying to say, he just doesn't know it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.