multiple preg_replace on the same variable
<p>While delving into some old code I've stumbled upon a function which is used to clean up special characters using <a href="http://de3.php.net/manual/en/function.preg-replace.php" rel="nofollow"><code>preg_replace</code></a> very excessively. Unfortunatly there's no way to avoid this cleanup, the messed up data arrive from outside.</p>

<pre><code>/* Example 1: current code */
$item = preg_replace('/' . chr(196) . '/',chr(142),$item);
$item = preg_replace('/' . chr(214) . '/',chr(153),$item);
$item = preg_replace('/' . chr(220) . '/',chr(154),$item);
</code></pre>

<p>Alot of those lines are in there, and all do the same (using different characters), so there should be a better way to do this. My first iteration to optimize this would be to use an indexed array, like this:</p>

<pre><code>/* Example 2: slightly optimized code */
$patterns = array();
$patterns[0] = '/'.chr(196).'/'; 
$patterns[1] = '/'.chr(214).'/';
$patterns[2] = '/'.chr(220).'/';

$reps = array();
$reps[0] = chr(142); 
$reps[1] = chr(153);
$reps[2] = chr(154);

$item = preg_replace($patterns,$reps,$item);
</code></pre>

<p><em>It does the job</em>, but I guess there's somewhere a better and/or faster way of doing this alot easier and smarter - maybe even without preg_replace. Due to the early morning and/or the lack of good coffee, I was unable to find it myself so far.</p>

<p>Any suggestions?</p>
