What I am trying to do is replace A#, A#m,… with another font-color version like this:
preg_replace('/\bA#m\b/','<span style="color:#910202;">A#m</span></a>',$content);
preg_replace('/\bA#\b/','<span style="color:#910202;">A#</span></a>',$content);
Unfortunately if A#m is the input it only styles the A# part. Any help? The /\b…\b/ method doesn’t appear to work.
The regular expressions work just fine. The problem is that the second one will also match the results of the first!
If you pass in
A#m, the first regex will change this to(which is not a valid HTML fragment by the way; what’s that
</a>doing there?)This will then be the input to the second regex. That one will see the
A#, replace it and the end result will beProbably not what you expect.
One solution would be to simply consolidate both expressions into one:
Update: If you want to make a regular expression that recognizes many different chords, use alternation:
Short explanation of
(A#(maj7|sus2|sus4)?)so that you can extend it: This regex matchesA#alone or optionally (due to the?) followed by one of (due to the|s)maj7,sus2,sus4. I don’t know what all the options are for chords, but it’s a good idea to read up on that reference/tutorial I link to. Learning just the basics gives you a lot of power to use.