sami
sami

Reputation: 7655

getting variables from STRING url in php

If I have a url that looks like this, what's the best way to read the value

http://www.domain.com/compute?value=2838

I tried parse_url() but it gives me value=2838 not 2838

Edit: please note I'm talking about a string, not an actual url. I have the url stored in a string.

Upvotes: 3

Views: 3053

Answers (5)

Adeel
Adeel

Reputation: 615

You should use GET method e.g

echo "value = ".$_GET['value'];  

Upvotes: 0

Christian Tellnes
Christian Tellnes

Reputation: 2080

You can use parse_url and then parse_str on the query.

<?php
$url = "http://www.domain.com/compute?value=2838";
$query = parse_url($url, PHP_URL_QUERY);
$vars = array();
parse_str($query, $vars);
print_r($vars);
?>

Prints:

Array
(
    [value] => 2838
)

Upvotes: 6

foxsoup
foxsoup

Reputation: 306

parse_str($url) will give you $value = 2838.

See http://php.net/manual/en/function.parse-str.php

Upvotes: 0

rik
rik

Reputation: 8622

$uri = parse_url($uri);
parse_str($uri['query'], $params = array());

Be careful if you use parse_str() without a second parameter. This may overwrite variables in your script!

Upvotes: 1

Harold
Harold

Reputation: 1166

For http://www.domain.com/compute?value=2838 you would use $_GET['value'] to return 2838

Upvotes: 3

Related Questions