Is there a way to use a backlink to a previous capture group as the name of a named capture group? This may not be possible; if not, this is the correct answer.
Following:
$data = 'description: some description'; preg_match("/([^:]+): (.*)/", $data, $matches); print_r($matches);
Productivity:
( [0] => description: some description [1] => description [2] => some description )
My attempt to use the backlink for the first capture group as a named capture group (?<$1>.*) Tells me that this is either not possible, or I'm just not doing it right:
preg_match("/([^:]+): (?<$1>.*)/", $data, $matches);
Productivity:
Warning: preg_match (): Compilation error: unrecognized character after (? <At offset 12
Desired Result:
( [0] => description: some description [1] => description [description] => some description )
This is simplified with preg_match . When using preg_match_all I usually use:
$matches = array_combine($matches[1], $matches[2]);
But thought that I could be smoother than that.
source share