很简单我似乎没有找到关于
PHP的preg_replace()支持命名反向引用的任何定义:
// should match,replace,and output: user/profile/foo $string = 'user/foo'; echo preg_replace('#^user/(?P<id>[^/]+)$#Di','user/profile/(?P=id)',$string);
它们存在:
原文链接:https://www.f2er.com/php/132989.htmlhttp://www.php.net/manual/en/regexp.reference.back-references.php
使用preg_replace_callback:
function my_replace($matches) { return '/user/profile/' . $matches['id']; } $newandimproved = preg_replace_callback('#^user/(?P<id>[^/]+)$#Di','my_replace',$string);
甚至更快
$newandimproved = preg_replace('#^user/([^/]+)$#Di','/user/profile/$1',$string);