I have url like below
/test/(?<name>\w+)/(?<id>\d+)/
I want to replace it using preg_replace() function like this
/test/name/(?<id>\d+)/
I tried this but it does not work as i expected.
$subject = '/test/(?<name>\w+)/(?<id>\d+)/';
preg_replace('#\(.*\<name\>.*\)#', 'name', $subject);
You could take all the characters until the next " )
" using [^)]+
.
$subject = '/test/(?<name>\w+)/(?<id>\d+)/';
$subject = preg_replace('#\(.*\<name\>[^)]+\)#', 'name', $subject);
echo $subject;
Outputs :
/test/name/(?<id>\d+)/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.