PHP某些元件内忽略的preg_match

问题描述 投票:1回答:1

我正在写一个regex,我需要过滤的内容进行格式化的排版。到目前为止,我的代码似乎是正确使用preg_replace过滤掉我的内容,但我无法弄清楚如何避免这种情况的某些标记内包裹的内容,说<pre>

作为参考,这是要的WordPress的the_content过滤器中使用,所以我当前的代码看起来像这样:

function my_typography( $str ) {
    $ignore_elements = array("code", "pre");

    $rules = array(
        "?" => array("before"=> "&thinsp;", "after"=>""),
        // the others are stripped out for simplicity
    );

    foreach($rules as $rule=>$params) {
        // Pseudo :
        //    if( !in_array( $parent_tag, $ignore_elements) {
        // /Pseudo


        $formatted = $params['before'] . $rule . $params['after'];
        $str = preg_replace( $rule, $formatted, $str );


        // Pseudo :
        //    }
        // /Pseudo
    }

    return $str;
}
add_filter( 'the_content',  'my_typography' );

基本上:

<p>Was this filtered? I hope so</p>
<pre>Was this filtered? I hope not.</pre> 

应该成为

<p>Was this filtered&thinsp;? I hope so</p>
<pre>Was this filtered? I hope not.</pre>
php regex preg-match
1个回答
1
投票

你需要用正则表达式搜索与正则表达式定界符preg_replace而且必须调用preg_quote逃避所有特殊的正则表达式字符,?.*+等:

$str = preg_replace( '~' . preg_quote($rule, '~') . '~', $formatted, $str );

全码:

function my_typography( $str ) {
    $ignore_elements = array("code", "pre");

    $rules = array(
        "?" => array("before"=> "&thinsp;", "after"=>""),
        // the others are stripped out for simplicity
    );

    foreach($rules as $rule=>$params) {
        // Pseudo :
        //    if( !in_array( $parent_tag, $ignore_elements) {
        // /Pseudo


        $formatted = $params['before'] . $rule . $params['after'];
        $str = preg_replace( '~' . preg_quote($rule, '~') . '~', $formatted, $str );


        // Pseudo :
        //    }
        // /Pseudo
    }

    return $str;
}

输出:

<p>Was this filtered&thinsp;? I hope so</p>
<pre>Was this filtered&thinsp;? I hope not.</pre>
© www.soinside.com 2019 - 2024. All rights reserved.