I’m running into a problem after giving preg_replace multiple replacement values to find. After the first find-and-replace, the function looks through the newly modified string and finds stuff that it shouldn’t.
This is what my code looks like:
[CODE]
$peptide_sequence = “A.TWSRKALIL.R”;
$patterns = array(“/(S)/i”,”/(D)/i”);//trying to search for S and D in above sequence
$replacements = array(“<font style=”color: red”>${1}</font>”,”<font style=”color: green”>${1}</font>”);//what i want to replace the above patterns with
$finished_peptide = preg_replace($patterns,$replacements,$peptide_sequence);
The problem with this setup is that it will find the S in the middle of the sequence, replace it with
[CODE]<font style=”color: red”>S</font>
just fine, but now the preg_replace is now searching the recently added text. It shouldn’t find D, nor do I want it to find D given that it’s not in the original sequence, but it finds the D in ‘red’.
I’m assuming that preg_replace iteratively goes through the replacements, so the way I have it setup clearly won’t work. Does anyone know of a regex that I can use to make sure that it isn’t searching inside tags. If I had such a regex, that would take care of my problem. So in conclusion, which regex filters out text that is contained inside html tags?