shayward
shayward

Reputation: 427

Url Explode for value

Currently I have a url thats like this,

http://website.com/type/value

I am using

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

this to get the value currently but my page has Facebook like's on it and when it is clicked it adds all these extra variables. http://website.com/type/value?fb_action_ids=1234567&fb_action_types= and that breaks that value that I am trying to get. Is there another way to get the specific value?

Upvotes: 0

Views: 189

Answers (3)

Tigger
Tigger

Reputation: 9130

You could also use:

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

Or, this:

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

With this, you do not need the trim() call or the temp var $url - unless you use it from something else.

The reason for two options is I don't know if /type/value is being passed to an index.php or if value is in fact a php file. Either way, one of the two options will give you what you need.

Upvotes: 0

Matt
Matt

Reputation: 1573

I'd use a preg_replace

explode('/', preg_replace('/?.*$/', '', $url));

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191809

Assuming you know that this will always be a valid URL, you can use parse_url.

list(, $value) = explode('/', parse_url($url)['path']);

Upvotes: 1

Related Questions