2

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

4 Answers 4

4
$array = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
Sign up to request clarification or add additional context in comments.

1 Comment

This method also has the benefit of removing trailing and preceding slashes.
1
<?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

Comments

1

use explode function

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

Comments

0

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");
}

1 Comment

actually, explode would be a better solution =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.