Code: Select all
<?php
$string = "123456-string";
preg_match("/^(.*?):(.*?)$/", $string, $matches);
print_r($matches);
?>Code: Select all
Array
(
[0] => 123456:string
[1] => 123456
[2] => string
)My desired result is simply:
Code: Select all
Array
(
[string] => 123456
)After I get the matches I can easily do this with some array functions, but I would rather not go through the extra steps if I can do it all with preg_match. So I have two questions.
1. If I use a named capture, can I avoid the duplicate results?
Code: Select all
/^(?P<name>.*?):(.*?)$/
RESULTS IN:
Array
(
[0] => 123456:string
[name] => 123456
[1] => 123456
[2] => string
)
I WOULD LIKE:
Array
(
[0] => 123456:string
[name] => 123456
[2] => string
)Code: Select all
preg_match("/^(?P<$1>.*?):(.*?)$/", $string, $matches);