I trying to get the values from a string by a regular expression pattern,
it works, but it will return all matched strings (I mean the string with {}
too)
this is the string:
dashboard/admin/{content}/category/{category}/posts
Regex pattern:
/{(.*?)}/
and the PHP code is :
preg_match_all('/{(.*?)}/', $url, $matches, PREG_SET_ORDER, 0);
and the content of $matches
is:
array:2 [
0 => array:2 [
0 => "{content}"
1 => "content"
]
1 => array:2 [
0 => "{category}"
1 => "category"
]
]
but I want to have an array like this:
array:2 [
0 => "content",
1 => "category"
]
Use lookaround:
$url = 'dashboard/admin/{content}/category/{category}/posts';
preg_match_all('/(?<={).*?(?=})/', $url, $matches, PREG_SET_ORDER, 0);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => content
)
[1] => Array
(
[0] => category
)
)