 <p>Pass by reference:</p> <pre><code>foreach ($posts as $post =&gt; &amp; $content) { $find = array('~\[image="(https?://.*?\.(?:jpg|jpeg|gif|png|bmp))"\](.*?)\[/image\]~s'); $replace = array('&lt;img src="$1" alt="" /&gt;&lt;p&gt;$2&lt;/p&gt;'); $content = preg_replace($find, $replace, $content); } </code></pre>