$url = '/article/math/unit2/chapter3/para4';
$pattern = "\/article";
preg_match_all('/^'.$pattern.'(?:\/([^\/]+))+$/', $url, $matches);
print_r($matches);
The output is
Array
(
[0] => Array
(
[0] => /article/math/unit2/chapter3/para4
)
[1] => Array
(
[0] => para4
)
)
Actually, I want to get an array as given below.
Array
(
[0] => math,
[1] => unit2,
[2] => chapter3,
[3] => para4
)
What is wrong with this code?
UPDATE2: The $pattern is dynamic one. may change to “/article/foo”, “/article/foo/bar”, etc.
You can simply use
explode()Reference: http://php.net/manual/en/function.explode.php
Above code will output,