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$acronymb(?!:)(?![^<>]*?>)|msU"
, "<acronym title="$fulltext">$acronym</acronym>"
, $text
);
You can use a PCRE SKIP/FAIL regex trick (also works in PHP) to tell the regex engine to only match something if it is not inside some delimiters:
This means: skip all substrings starting with
<pre>
and ending with</pre>
, and only then match$acronym
as a whole word.See demo on regex101.com
Here is a sample PHP demo:
Output:
It is also possible to use
preg_split
and keep the code block as a group, only replace the non-code block part then combine it back as a complete string:Taken from here.