I have a URL that is hardcoded via a config which I extract into a config:$url = 'http://www.example.com/api/v1/all/limit/{limit}/offset/{offset}' // this is stored as a string inside a config filethen $limit = 10; $offset = 0;I currently use the following preg_replace to replace the given URL and transform into an effective URL:$url = preg_replace('/\{([A-Z, a-z]+)\}/e', $$1, $url);The effective string is now:http://www.example.com/api/v1/all/limit/10/offset/0It works, however, I had to dig deep into figuring out the pattern and $$1 part so there goes readability.My questions:Is there anything else out there that someone can think of that can be as good as the above code?I could use a str_replace with 2 arrays and do a replacement of the variables - it might be cleaner but it maybe slower.  Would anyone advise replacing that theory instead of this?  If so, why?The number of variables could grow - currently doing this it will capture all $variable there is within that scope - so any suggestion would need to keep in mind that limit/offset will not be the only limited variables that will be incoming:$url = 'http://www.example.com/api/v1/all/id/{id}/limit/{limit}/offset/{offset}'$url = 'http://www.example.com/api/v1/all/relatedId/{relatedId}/limit/{limit}/offset/{offset}'
Cleaner or better readability in URL composition
php;optimization;regex;url
If you're going after readability and speed, then doing a preg_replace is actually a lot slower than doing a str_replace. I'm using this test code:$original_url = 'http://www.example.com/api/v1/all/limit/{limit}/offset/{offset}' ;$limit = 10;$offset = 0;$preg_start = microtime();$url = preg_replace('/\{([A-Z, a-z]+)\}/e', $$1, $original_url);$preg_end = microtime();$preg = $preg_end - $preg_start;$str_start = microtime(true);$url = str_replace(array({limit}, {offset}), array($limit, $offset), $original_url);$str_end = microtime(true);$str_a = $str_end - $str_start;$str_start = microtime(true);$url = str_replace({limit}, $limit, $original_url);$url = str_replace({offset}, $offset, $url);$str_end = microtime(true);$str_b = $str_end - $str_start;echo $preg . \n . $str_a . \n . $str_b . \n;echo '$preg > $str_a: '  . ($preg > $str_a ? true : false)  . \n;echo '$preg > $str_b: '  . ($preg > $str_b ? true : false)  . \n;echo '$str_a > $str_b: ' . ($str_a > $str_b ? true : false) . \n;And I'm getting these results:6.4000000000008E-51.4066696166992E-53.0994415283203E-6$preg > $str_a: true$preg > $str_b: true$str_a > $str_b: trueSo, ultimately, using two function calls to str_replace() without arrays is faster overall than preg_replace and str_replace with an array. EDITConsidering your newly added third point (non-fixed number of variables), it would be best to abstract this to a function, at the sacrifice of speed. function replace(array $replacements, $url){    foreach($replacements as $template => $value){        $url = str_replace($template, $value, $url);    }    return $url;}$url = 'http://www.example.com/api/v1/all/relatedId/{relatedId}/limit/{limit}/offset/{offset}';$replacements = array({relatedId} => 231, {limit} => 10, {offset} => 10);$start = microtime(true);echo replace($replacements, $url);$end = microtime(true);echo \n . ($end - $start);Results in:http://www.example.com/api/v1/all/relatedId/231/limit/10/offset/102.4795532226562E-5Which is comparable in speed to the multiple str_replace call (which is what you're technically doing in the function. 