Is there a way using regex to replace characters in a string based on position?
For instance, one of my rewrite rules for a project I’m working on is “replace o with ö if o is the next-to-last vowel and even numbered (counting left to right).”
So, for example:
heabatoikwould becomeheabatöik(ois the next-to-last vowel, as well as the fourth vowel)habatoikwould not change (ois the next-to-last vowel, but is the third vowel)
Is this possible using preg_replace in PHP?
Starting with the beginning of the subject string, you want to match 2n + 1 vowels followed by an
o, but only if theois followed by exactly one more vowel:To do the same but for an odd-numbered
o, match 2n leading vowels rather than 2n + 1:If one doesn’t match, then it performs no replacement, so it’s safe to run them in sequence if that’s what you’re trying to do.