Php 带有Posix排序元素的正则表达式中的警告

Php 带有Posix排序元素的正则表达式中的警告,php,regex,Php,Regex,我试图在php中运行以下基于正则表达式的函数,最终返回输出 function vg_excerpt_more( $output ) { $string = $output; $pattern_auto_excerpt = '#([...]</p>)$#'; $pattern_manual_excerpt = '#(</p>)$#'; $replacement = ' <a href="'.get_permalink().'"&g

我试图在php中运行以下基于正则表达式的函数,最终返回输出

function vg_excerpt_more( $output ) {

    $string = $output;

    $pattern_auto_excerpt = '#([...]</p>)$#';
    $pattern_manual_excerpt = '#(</p>)$#';

    $replacement = ' <a href="'.get_permalink().'">[Continue...]</a></p>';

    if ( preg_match( $pattern_auto_excerpt, $string ) ) {

        $pattern = $pattern_auto_excerpt;

    } else if ( preg_match( $pattern_manual_excerpt, $string ) ) {

        $pattern = $pattern_manual_excerpt;

    }   

    $output = preg_replace( $pattern, $replacement, $string );

    return $output;

}
add_filter( 'the_excerpt', 'vg_excerpt_more' );
add_filter( 'excerpt_more', 'vg_excerpt_more' );
函数vg\u摘录\u更多($output){
$string=$output;
$pattern_auto_extract='#([…])

)$#'; $pattern_manual_摘录='#(

)$#'; $replacement='

'; if(preg_match($pattern_auto_extract,$string)){ $pattern=$pattern\u auto\u摘录; }else if(preg_match($pattern_manual_摘录,$string)){ $pattern=$pattern\u manual\u摘录; } $output=preg_replace($pattern,$replacement,$string); 返回$output; } 添加过滤器('the_extract'、'vg_extract\u more'); 添加_过滤器('extract_more','vg_extract_more');
嗯,字符串可以以
[…]

结尾,因此我必须检查这两个案例

问题是,它会发出警告-

警告:PREG_MATCH():编译失败:POSIX排序元素 “预匹配($pattern\u auto\u摘录)中的偏移量1不支持, $string)'

警告:PREG_REPLACE():在“$output”中为空正则表达式= preg_replace($pattern,$replacement,$string);'

编辑:

在@user1852180提供了有用的回复之后,我继续前进并做了这件事-

function vg_excerpt_more( $output ) {


    $string = $output;

    $pattern = '';

    // $pattern_auto_excerpt = '#(\[...\]</p>)$#';

    $pattern_auto_excerpt = '#(\[(?:\.|…)+\])#';
    $pattern_manual_excerpt = '#(</p>)$#';

    $replacement = ' <a href="'.get_permalink().'">[Continue...]</a></p>';

    if ( preg_match( $pattern_auto_excerpt, $string ) ) {

        $pattern = '#(\[(?:\.|…)+\]</p>)$#';

        if ( preg_match( $pattern, $string ) ) {
            return preg_replace( $pattern, $replacement, $string ) . "Dummy2";
        }

    } else if ( preg_match( $pattern_manual_excerpt, $string ) ) {

        $pattern = $pattern_manual_excerpt;
        return preg_replace( $pattern, $replacement, $string ) . "Dummy";

    }

    return $output;

}
add_filter( 'the_excerpt', 'vg_excerpt_more' );
add_filter( 'excerpt_more', 'vg_excerpt_more' );
函数vg\u摘录\u更多($output){
$string=$output;
$pattern='';
//$pattern\u auto\u extract='#(\[…\]

)$#'; $pattern_auto_extract='#(\[(?:\.\124;…)+\])#'; $pattern_manual_摘录='#(

)$#'; $replacement='

'; if(preg_match($pattern_auto_extract,$string)){ $pattern='#(\[(?:\.\124;…)+\]

)$#'; if(preg_匹配($pattern,$string)){ 返回preg_replace($pattern,$replacement,$string)。“Dummy2”; } }else if(preg_match($pattern_manual_摘录,$string)){ $pattern=$pattern\u manual\u摘录; 返回preg_replace($pattern,$replacement,$string)。“Dummy”; } 返回$output; } 添加过滤器('the_extract'、'vg_extract\u more'); 添加_过滤器('extract_more','vg_extract_more');
但是我仍然看到了前端的
[…]
以及替换件


另外,它也从不打印“Dummy2”,总是“Dummy”。

您需要避开第一个图案中的括号和圆点:

$pattern_auto_excerpt = '#(\[(?:\.|…)+\]</p>)$#';
$pattern\u auto\u extract='#(\[(?:\.\124;…)+\]

)$#';
您不需要使用if/else来检查它是否有[…],让正则表达式用问号检查:

function vg_excerpt_more( $output ) {

    $pattern = '#(?:\[(?:\.|…)+\])?</p>$#';
    $replacement = ' <a href="'.get_permalink().'">[Continue...]</a></p>';

    return preg_replace( $pattern, $replacement, $output );

}
函数vg\u摘录\u更多($output){
$pattern='#(?:\[(?:\.\124;…)+\])?

$#'; $replacement='

'; 返回preg_replace($pattern,$replacement,$output); }
谢谢您的回复。在我的例子中,字符串可以以
[…]

结尾,也可以仅以

结尾。很抱歉让人困惑,我也编辑了主要问题。您的代码会检查这两种情况吗?我的第一个代码应该通过转义括号来修复您的警告。我的第二个代码是一个与您的代码相同的较小代码的示例,它在正则表达式模式中使用问号,因此它将处理以[…]结尾的字符串。

并且只

。谢谢。但在测试中,我发现替换后仍会显示
[…]
。我看到一些WordPress使用省略号(三点标点符号:
)。您可以尝试在正则表达式模式中用
\[(?:\.\124;…)+\]
替换
\[…]
,它将匹配省略号和三个点。当使用字符串
[…]

测试新代码时,我得到Dummy2。