<?php
// Here is a url that contains some regex strings (see the (\d+) twice)?
// I want to replace each one individually, using a regex of the regex, with the contents of the $params array
// Final result should be http://whatever.com/en-GB/companies/8556/blah/502034/link/etc
// Note they're in the correct order!
$expectedUrl = "http://whatever.com/en-GB/companies/(\d+)/blah/(\d+)/link/etc";
preg_match_all('#\((.*?)\)#', $expectedUrl, $matches);
$search = $matches[0];
$params = array(8556, 50234);
foreach (array_combine($params, $search) as $value => $key)
{
$split = str_split($key);
foreach ($split as &$char) {
if ($char !== "\\") $char = "\\" . $char;
}
$split = implode("", $split);
$expectedUrl = preg_replace("/".$split."/", $value, $expectedUrl, 1);
}
var_dump($expectedUrl);