For some reason my preg_replace() is only replacing the first matched $pattern in the $subject. Before this the $pattern and $replacement were arrays and the second half worked flawlessly to replace all the matches, however I separated it out to try to isolate the problem. No luck though. Any advice would be greatly appreciated.
This is the snippet of $subject that is the target of this replacement.
<a href="/w/index.php?title=Special:Search&search=blaz&fulltext=Search&offset=20&limit=20">2</a> <a href="/w/index.php?title=Special:Search&search=blaz&fulltext=Search&offset=40&limit=20">3</a> <a href="/w/index.php?title=Special:Search&search=blaz&fulltext=Search&offset=60&limit=20">4</a> <a href="/w/index.php?title=Special:Search&search=blaz&fulltext=Search&offset=80&limit=20">5</a> <a href="/w/index.php?title=Special:Search&search=blaz&fulltext=Search&offset=20&limit=20"><span style='font-size: small'>Next</span> »</a>
This is the pattern, replacement, and preg_replace() code.
$pattern = "/<a href=\"\/w\/index\.php\?title=Special:Search&search=(.+)&fulltext=Search&offset=(\d+)&limit=(\d+)\">(.+)<\/a>/";
$replace = '<a href="' . $_SERVER['PHP_SELF'] . '?submit=true&fulltext=Search&search=${1}&offset=${2}&limit=${3}">${4}</a>';
$result = preg_replace($pattern, $replace, $result);