Php 避免在文本带有特定标记时将文本缠绕在代码标记周围

Php 避免在文本带有特定标记时将文本缠绕在代码标记周围,php,regex,Php,Regex,我正在使用一个Wordpress插件,它将代码标记放在背景标记中的文本周围,以便在我的博客上突出显示。原来的功能是: function mdc_the_content( $content ) { $content = preg_replace( '/`(.*?)`/', '<code>$1</code>', $content ); return $content; } // mdc_the_content add_filter( 'the_conten

我正在使用一个Wordpress插件,它将代码标记放在背景标记中的文本周围,以便在我的博客上突出显示。原来的功能是:

function mdc_the_content( $content ) {

    $content = preg_replace( '/`(.*?)`/', '<code>$1</code>', $content );
    return $content;

} // mdc_the_content
add_filter( 'the_content', 'mdc_the_content' );
当我使用js highlighter插件(code highlighter)时,它会在backticks中的代码周围放置代码标签。我想避免这种情况。下面的功能可以工作吗?仅当反勾号未包装在
$1
中时,才在标记周围放置代码标记。下面的if语句有效吗?有时荧光笔内可能有多个对象缠绕在背景标记周围。多谢各位

function mdc_the_content( $content ) {

    if(!preg_match('/`(.*?)`/', '<pre class="EnlighterJSRAW" data-enlighter-language="r">$1</pre>', $content)) {
        $content = preg_replace( '/`(.*?)`/', '<code>$1</code>', $content );
    }

    return $content;

} // mdc_the_content
add_filter( 'the_content', 'mdc_the_content' );
你可以用

$content=preg_replace(“~.*(*SKIP)(*F)`(.*?“~s)”,“
$1
”,$content);

*?(*SKIP)(*F)
部分匹配带有两个特定属性的
pre
标记,允许任意数量的空白和其中的所有内容,
`(*?)
将在所有其他上下文中匹配