PHP-URL to array
Предположим, что URL-адрес выглядит следующим образом: http://www.example.com/category/product/htc/desire, я использовал $_SERVER['REQUEST_URI']
, чтобы получить /category / product / htc / desire, как я могу преобразовать этот"/category/product/htc / desire " в массив типа:
array
(
[0] => category
[1] => product
....
)
Спасибо
4 ответа:
<?php $url = "/category/product/htc/desire"; $pieces = explode("/", substr($url,1)); print_r($pieces); ?>
Очевидно, что $ url будет $_SERVER ['REQUEST_URI']
Вывод, смотрите здесь: http://codepad.org/lIRZNTBI
Взгляните на функцию PHP strtok Вы можете сделать что-то вроде этого:
$string = "/category/product/htc/desire"; $arr = aray(); $tok = strtok($string, "/"); while ($tok !== false) { arr[]= $tok: $tok = strtok(" \n\t"); }