PHP Regex expression excluding <pre> tag -


i using wordpress plugin named acronyms (https://wordpress.org/plugins/acronyms/). plugin replaces acronyms description. uses php preg_replace function.

the issue replaces acronyms contained in <pre> tag, use present source code.

could modify expression won't replace acronyms contained inside <pre> tags (not directly, in moment)? possible?

the php code is:

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

you can use pcre skip/fail regex trick (also works in php) tell regex engine match if not inside delimiters:

(?s)<pre[^<]*>.*?<\/pre>(*skip)(*f)|\b$acronym\b 

this means: skip substrings starting <pre> , ending </pre>, , match $acronym whole word.

see demo on regex101.com

here sample php demo:

<?php $acronym = "ascii"; $fulltext = "american standard code information interchange"; $re = "/(?s)<pre[^<]*>.*?<\\/pre>(*skip)(*f)|\\b$acronym\\b/";  $str = "<pre>ascii\nsometext\nmoretext</pre>more text \nascii\nmore text<pre>more\nlines\nascii\nlines</pre>";  $subst = "<acronym title=\"$fulltext\">$acronym</acronym>";  $result = preg_replace($re, $subst, $str); echo $result; 

output:

<pre>ascii</pre><acronym title="american standard code information interchange">ascii</acronym><pre>ascii</pre> 

Comments