I have a great little script that will search a file and replace a list of words with their matching replacement word. I have also found a way to prevent preg_replace from replacing those words if they appear in anchor tags, img tags, or really any one tag I specify. I would like to create an OR statement to be able to specify multiple tags. To be clear, I would like to prevent preg_replace from replacing words that not only appear in an anchor tag, but any that appear in an anchor,link,embed,object,img, or span tag. I tried using the '|' OR operator at various places in the code with no success.
- <?php
-
$data = 'somefile.html';
-
$data = file_get_contents($data);
-
$search = array ("/(?!(?:[^<]+>|[^>]+<\/a>))\b(red)\b/is","/(?!(?:[^<]+>|[^>]+<\/a>))\b(white)\b/is","/(?!(?:[^<]+>|[^>]+<\/a>))\b(blue)\b/is");
-
$replace = array ('Apple','Potato','Boysenberry');
-
echo preg_replace($search, $replace, $data);?>
-
print $data;
-
?>
looking at the first search term which basically says to search for "red" but not inside <a> </a>:
- "/(?!(?:[^<]+>|[^>]+<\/a>))\b(red)\b/is"
I am trying to figure out how I can keep preg_replace out of not just the </a> tag, but also the other tags listed.