开发者

PHP - URL to array

开发者 https://www.devze.com 2023-02-17 23:40 出处:网络
Suppose I have the URL look like: http://www.example.com/category/product/htc/desire, I used $_SERVER[\'REQUEST_URI\'] to get /category/product/htc/desire, how can I convert this \"/category/product/h

Suppose I have the URL look like: http://www.example.com/category/product/htc/desire, I used $_SERVER['REQUEST_URI'] to get /category/product/htc/desire, how can I convert this "/category/product/htc/desire" to array like:

array
(
[0] => category
[1] => product
....开发者_开发问答
)

Thanks


$array = explode('/', trim($_SERVER['REQUEST_URI'], '/'));


<?php

$url  = "/category/product/htc/desire";
$pieces = explode("/", substr($url,1));

print_r($pieces);

?>

obviously $url would be the $_SERVER['REQUEST_URI']

output, see here: http://codepad.org/lIRZNTBI


use explode function

$list = explode('/', trim($_SERVER['REQUEST_URI'], '/'));


Have a look at PHP strtok function You can do something like that :


$string = "/category/product/htc/desire";
$arr = aray();
$tok = strtok($string, "/");

while ($tok !== false) {
    arr[]= $tok:
    $tok = strtok(" \n\t");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消