In my PHP script, I want to use preg_replace to replace values, skipping any value that is within the HTML tags <a>
and </a>
. The values are contained within arrays, so the following is a snippet of the current code that is run within a loop:
$Used[$i] = preg_replace("~<a[^>]*>.*?</a\s*>(*SKIP)(*FAIL)|{$MatchFinal[$i]}~","{$MatchLinkFinal[$i]}", $value);
The code works, except for values that contain parentheses (
or )
.
For example, if $MatchFinal[$i]
contains the value Fatigue
, then it replaces it with a value $MatchLinkFinal[$i]
that includes a link: <a>
...Fatigue
...<a>
. However, if $MatchFinal[$i]
contains the value Hair (Dry)
, then it does not replace it.
Why are values that contain parentheses not being replaced?
Note: Values that contain spaces and/or apostrophes are being replaced.
I tried escaping the parentheses...
$MatchFinal[$i] = str_replace("(", "\(", $MatchFinal[$i]);
and
$MatchFinal[$i] = str_replace(")", "\)", $MatchFinal[$i]);
...but I received an error message:
Warning: preg_replace(): Compilation failed: missing ) at offset 51