An interesting call, I encoded something that works for this example, note that for this code you need PHP 5.3+:
$regex = '^page/(?P<id>\d+)-(?P<slug>[\.]+)\.html$'; $args = array( 'id' => 5, 'slug' => 'my-first-article' ); $result = preg_replace_callback('#\(\?P<(\w+)>[^\)]+\)#', function($m)use($args){ if(array_key_exists($m[1], $args)){ return $args[$m[1]]; } }, $regex); $result = preg_replace(array('#^\^|\$$#', '#\\\\.#'), array('', '.'), $result);
Output: page/5-my-first-article.html
online demo .
Hamza source share