I'm working on a REST styled API, and I want to be able to break the URL down into individual variables.
Say I have the following URL: www.example.com/user/post/1
I'd like to make the following variables:
$uri_1 = user
$uri_2 = post
$uri_3 = 1
I tried to do this but it got stuck in a loop
$path = explode('/', $this->path($uri));
for($i=0;$i < count($path);$i++){
$uri_.$i = $path[i];
}
$url = explode('/', strtolower(trim($_SERVER['REQUEST_URI'], '/')));
$uri_1 = isset($url[0])?$url[0]:'';
$uri_2 = isset($url[1])?$url[1]:'';
$uri_3 = isset($url[2])?$url[2]:'';
Here's how you do it for an arbitrary number of variables, using PHP's variable variables feature:
$path = explode('/', $this->path($uri));
for($i=0;$i < count($path);$i++){
${"uri_".$i} = $path[i];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.