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

asked Mar 19, 2011 at 1:40

4 Answers 4

4
$array = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
answered Mar 19, 2011 at 1:42
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

answered Mar 19, 2011 at 1:43

Comments

1

use explode function

$list = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
BenMorel
37.1k53 gold badges208 silver badges339 bronze badges
answered Mar 19, 2011 at 1:42

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");
}
answered Mar 19, 2011 at 1:46

1 Comment

actually, explode would be a better solution =)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.