php – 使用preg_replace命名反向引用

很简单我似乎没有找到关于 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);

这是一个微不足道的例子,但我想知道是否不支持这种语法(?P = name).语法问题或不存在的功能

它们存在:

http://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);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...