PHP REGEX preg_replace_callback不匹配

时间:2018-01-02 08:19:34

标签: php regex

我正在尝试匹配

[history =" e70b3ffc-beaf-423f-b084-72bc5bae3147"]历史名称[/ history] ​​

获取"标签"之间的ID和内容。虽然上面的https://regexr.com/3ilt2上的工作正常,但它在我的代码上不匹配,我无法弄清楚原因。有任何想法吗?

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

1 个答案:

答案 0 :(得分:1)

preg_replace_callback函数在以下代码中正常工作:

$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;

这将输出捕获的UUID e70b3ffc-beaf-423f-b084-72bc5bae3147。您的parseHistories()功能一定存在问题。

Demo

相关问题