Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP正则表达式preg_replace_回调不匹配_Php_Regex - Fatal编程技术网

PHP正则表达式preg_replace_回调不匹配

PHP正则表达式preg_replace_回调不匹配,php,regex,Php,Regex,我正在努力匹配 [history=“e70b3ffc-beaf-423f-b084-72bc5bae3147”]此处的历史名称[/history] 并获取“标签”之间的ID和内容。虽然上面的代码在我的代码中并不匹配,但我不知道为什么。有什么想法吗 $parsed_string = preg_replace_callback( // [history="ID"]ABC[/history] '/\[history="(.*?)"\](.*?)\[\/histo

我正在努力匹配

[history=“e70b3ffc-beaf-423f-b084-72bc5bae3147”]此处的历史名称[/history]

并获取“标签”之间的ID和内容。虽然上面的代码在我的代码中并不匹配,但我不知道为什么。有什么想法吗

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

preg\u replace\u回调函数在以下代码中运行良好:

$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()
函数肯定有问题


您的代码很好,除了
parseHistories
之外,我假设它做错了什么。可能会显示丢失的代码。您在[history=它本来不在那里,在UUID之后,我试图传递的字符串不应该有它。您正在转义吗?我有点困惑。我可以看到上面的操作正常,但我不能understand@DimitrisRomeoHavlidis我只是剪贴了你的图案,仅此而已。他说的是主题字符串。是的@DimitrisRomeoHavlidis,是escaping.@Revo我很惊讶他的代码甚至可以在双引号字符串中不转义双引号的情况下运行。我知道你想说什么,他就是不知道。