/    Sign up×
Community /Pin to ProfileBookmark

preg_replace question

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);
[/CODE]

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>[/CODE]

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?

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @ternto333 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...