简体   繁体   English

php preg_replace 如何排除案例

[英]php preg_replace how to exclude case

I have the following preg_replace我有以下preg_replace

foreach ($financeTermsArray as $term => $info) {

   $content = preg_replace($term, $info, $content, 1);
}

where the $info variable looks like $info 变量的样子

'<a class="glossar-word" href="'. get_permalink(). '" data-glossar="'. $excerpt. '">'. get_the_title(). '</a>';

and $term variable like "/\b". get_the_title(). "\b/"$term变量,如"/\b". get_the_title(). "\b/" "/\b". get_the_title(). "\b/"

I want to exclude from preg_replace if matched word is inside data-glossar html attribute.如果匹配的单词在data-glossar html 属性中,我想从preg_replace中排除。

How would I do this?我该怎么做?

I got it working with the following pattern:我让它使用以下模式:

"/data-glossar=\"([^\"]*)(*SKIP)(*F)|\b". get_the_title(). "\b/"

Here is a regex that is constructed with the get_the_title() at runtime.这是一个在运行时使用get_the_title()构造的正则表达式。
When the regex is constructed, it will SKIP ALL TAGS in html, including构造正则表达式时,它将跳过html 中的所有标签,包括
invisible content, and will only match the '\b'. get_the_title(). '\b'不可见的内容,并且只会匹配'\b'. get_the_title(). '\b' '\b'. get_the_title(). '\b'
as found OUTSIDE tags.如发现的外部标签。

I don't have anyway to test out the resulting regex string, so you should do that我无论如何都不需要测试生成的正则表达式字符串,所以你应该这样做
first and see if it looks ok.首先,看看它看起来是否正常。

Use this on a preg_replace as originally intended.按照最初的预期在 preg_replace 上使用它。

$term = '~<(?:(?:(?:(script|style|object|embed|applet|noframes|noscript|noembed)(?:\s+(?>"[\S\s]*?"|\'[\S\s]*?\'|(?:(?!/>)[^>])?)+)?\s*>)[\S\s]*?</\1\s*(?=>))|(?:/?[\w:]+\s*/?)|(?:[\w:]+\s+(?:"[\S\s]*?"|\'[\S\s]*?\'|[^>]?)+\s*/?)|\?[\S\s]*?\?|(?:!(?:(?:DOCTYPE[\S\s]*?)|(?:\[CDATA\[[\S\s]*?\]\])|(?:--[\S\s]*?--)|(?:ATTLIST[\S\s]*?)|(?:ENTITY[\S\s]*?)|(?:ELEMENT[\S\s]*?))))>(*SKIP)(*FAIL)|'
        . '\b' . get_the_title() . '\b~';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM