PHP Regex表达式,不包括<pre> tag

时间:2015-05-08 09:51:48

标签: php regex wordpress replace pre

I am using a WordPress plugin named Acronyms (https://wordpress.org/plugins/acronyms/). This plugin replaces acronyms with their description. It uses a PHP PREG_REPLACE function.

The issue is that it replaces the acronyms contained in a <pre> tag, which I use to present a source code.

Could you modify this expression so that it won't replace acronyms contained inside <pre> tags (not only directly, but in any moment)? Is it possible?

The PHP code is:

$text = preg_replace(
    "|(?!<[^<>]*?)(?<![?.&])\b$acronym\b(?!:)(?![^<>]*?>)|msU"
  , "<acronym title=\"$fulltext\">$acronym</acronym>"
  , $text
);

1 个答案:

答案 0 :(得分:0)

也可以使用preg_split并将代码块保留为一个组,只替换非代码块部分,然后将其组合回一个完整的字符串:

function replace($s) {
    return str_replace('"', '&quot;', $s); // do something with `$s`
}

$text = 'Your text goes here...';
$parts = preg_split('#(<\/?[-:\w]+(?:\s[^<>]+?)?>)#', $text, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$text = "";
$x = 0;
foreach ($parts as $v) {
    if (trim($v) === "") {
        $text .= $v;
        continue;
    }
    if ($v[0] === '<' && substr($v, -1) === '>') {
        if (preg_match('#^<(\/)?(?:code|pre)(?:\s[^<>]+?)?>$#', $v, $m)) {
            $x = isset($m[1]) && $m[1] === '/' ? 0 : 1;
        }
        $text .= $v; // this is a HTML tag…
    } else {
        $text .= !$x ? replace($v) : $v; // process or skip…
    }
}

return $text;

取自here