Req #81469 [Opn->Csd]: preg_replace(): add support for named capture groups

From: Date: Fri, 24 Sep 2021 05:29:44 +0000
Subject: Req #81469 [Opn->Csd]: preg_replace(): add support for named capture groups
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-236790@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81469&edit=1 ID: 81469 User updated by: wim at squeezely dot tech Reported by: wim at squeezely dot tech Summary: preg_replace(): add support for named capture groups -Status: Open +Status: Closed Type: Feature/Change Request Package: PCRE related PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Okay, I'll use preg_replace_callback instead. I just thought it would be nice if preg_replace() would support the capabilities of regex completely. But it makes sense to spend time on other things. Thanks! Previous Comments: ------------------------------------------------------------------------ [2021-09-23 15:57:32] nikic@php.net TBH I don't think this is worthwhile anymore at this point. With preg_replace_callback you can do: preg_replace_callback('/(?<namedGroup>[a-z]+)$/', fn($m) => "ice-cream-with-$m[namedGroup]", 'I-like-chocolate-sprinkles')); Which is not much worse than dedicated syntax, and gives you the full generality of PHP without any questions about edge-case behavior. ------------------------------------------------------------------------ [2021-09-23 13:01:44] wim at squeezely dot tech Description: ------------ Would it be possible to add support for named capture groups to preg_replace()? php > var_dump(preg_replace('/([a-z]+)$/', 'ice-cream-with-${1}', 'I-like-chocolate-sprinkles')); string(41) "I-like-chocolate-ice-cream-with-sprinkles" Add support for: var_dump(preg_replace('/(?<namedGroup>[a-z]+)$/', 'ice-cream-with-${namedGroup}', 'I-like-chocolate-sprinkles')); Test script: --------------- php > var_dump(preg_replace('/(?<namedGroup>[a-z]+)$/', 'ice-cream-with-${namedGroup}', 'I-like-chocolate-sprinkles')); Expected result: ---------------- string(41) "I-like-chocolate-ice-cream-with-sprinkles" Actual result: -------------- string(45) "I-like-chocolate-ice-cream-with-${namedGroup}" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81469&edit=1

« previous php.bugs (#236790) next »