Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP preg_replace multiple rules
    primarykey
    data
    text
    <p>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.</p> <p><strong>Input:</strong></p> <pre><code>$string = 'The quick brown fox jumps over the lazy freaky dog'; </code></pre> <p><strong>Rules:</strong> </p> <ul> <li>Replace <strong>a</strong>, <strong>i</strong>, <strong>o</strong> with <strong>u</strong> (if not at the beginning of a word &amp; if not before/after a vowel)</li> <li>Replace <strong>e</strong>, <strong>u</strong> with <strong>i</strong> (if not at the beginning of a word &amp; if not before/after a vowel)</li> <li>Replace <strong>ea</strong> with <strong>i</strong> (if not at beginning of a word)</li> <li>Replace whole words ie <strong>dog</strong> with <strong>cat</strong> and <strong>fox</strong> with <strong>wolf</strong> (without applying the rules above)</li> </ul> <p><strong>Output:</strong> Thi quick bruwn wolf jimps over thi luzy friky cat</p> <p><br><br><br> I started with something like that: (<em>Edited thanks to Ezequiel Muns</em>)</p> <pre><code>$patterns = array(); $replacements = array(); $patterns[] = "/(?&amp;lt;!\b|[aeiou])[aio](?![aeiou])/"; $replacements[] = "u"; $patterns[] = "/(?&amp;lt;!\b|[aeiou])[eu](?![aeiou])/"; $replacements[] = "i"; $patterns[] = '/ea/'; $replacements[1] = 'i'; $patterns[] = '/dog/'; $replacements[0] = 'cat'; echo preg_replace($patterns, $replacements, $string); </code></pre> <p><strong>Output:</strong> </p> <pre><code>Thi qiick briwn fix jimps ivir thi lizy friiky dig </code></pre> <p><br></p> <hr> <p><em>Edited:</em></p> <p>As you can see the problem is that <strong>every rule gets overwritten by the previous rule</strong>.</p> <p>Example '<strong>fox</strong>':</p> <ol> <li>rule: turns <strong>fox</strong> into <strong>fux</strong></li> <li>rule: turns <strong>fux</strong> into <strong>fix</strong></li> </ol> <p>Is there a way to avoid the following rule(s) if the character was already been effected by the previous rule?</p> <p>Does this makes sense?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload