Matches in url regex
Posted: Tue Oct 07, 2014 12:04 pm
Hi 
I want to parse any url that separates string with '/'
For example: /home/index/3
or /users/16
So i wrote the following code:
I expect the result for /home/index/3 to be:
array (0 => "home", "1" => "index", 2 => "3")
But the actual result is: array (0 => "/home/index/3", 1 => "/3", 2 => "3")
It completely ignores home and index...
Can someone tell me please what i had done wrong?
Thanks
I want to parse any url that separates string with '/'
For example: /home/index/3
or /users/16
So i wrote the following code:
Code: Select all
<?php
$url = Some input...;
$regex = "(/([a-zA-Z0-9_]+))+";
if(preg_match("@^" . $regex . "$@", $url, $matches)) {
echo "<pre>";
print_r($matches);
echo "</pre>";
}
?>array (0 => "home", "1" => "index", 2 => "3")
But the actual result is: array (0 => "/home/index/3", 1 => "/3", 2 => "3")
It completely ignores home and index...
Can someone tell me please what i had done wrong?
Thanks