Hello my second family, :)
I'm just wondering how to apply several rules for a preg_replace without executing them in the first run. Its a bit complicated let me explain based on an example.
Input:
$string = 'The quick brown fox jumps over the lazy freaky dog';
Rules:
- Replace a, i, o with u (if not at the beginning of a word & if not before/after a vowel)
- Replace e, u with i (if not at the beginning of a word & if not before/after a vowel)
- Replace ea with i (if not at beginning of a word)
- Replace whole words ie dog with cat and fox with wolf (without applying the rules above)
Output:
Thi quick bruwn wolf jimps over thi luzy friky cat
I started with something like that: (Edited thanks to Ezequiel Muns)
$patterns = array(); $replacements = array(); $patterns[] = "/(?<!\b|[aeiou])[aio](?![aeiou])/"; $replacements[] = "u"; $patterns[] = "/(?<!\b|[aeiou])[eu](?![aeiou])/"; $replacements[] = "i"; $patterns[] = '/ea/'; $replacements[1] = 'i'; $patterns[] = '/dog/'; $replacements[0] = 'cat'; echo preg_replace($patterns, $replacements, $string);
Output:
Thi qiick briwn fix jimps ivir thi lizy friiky dig
Edited:
As you can see the problem is that every rule gets overwritten by the previous rule.
Example 'fox':
- rule: turns fox into fux
- rule: turns fux into fix
Is there a way to avoid the following rule(s) if the character was already been effected by the previous rule?
Does this makes sense?