1
$url= "justfwalk.it?uid=12";

How can I get the variable "uid" from this string?

Is there is an easier method than looking for it's position and trimming the string?

3
  • 1
    php.net/manual/en/function.parse-url.php Commented Nov 16, 2011 at 17:53
  • @Brad Note: This function doesn't work with relative URLs. Commented Nov 16, 2011 at 17:54
  • 2
    @Neal, but it is trivial to concatenate a protocol and hostname to the front to use this function. Or, split on ? and use php.net/manual/en/function.parse-str.php Commented Nov 16, 2011 at 17:56

4 Answers 4

5

How about parse_url and parse_str?

<?php
$params = array();
$url= "justfwalk.it?uid=12";
$url_parts = parse_url( $url);
parse_str( $url_parts['query'], $params);
echo $params['uid']; // Outputs 12

See it in action

Sign up to request clarification or add additional context in comments.

Comments

2

Sure, use parseurl

<?php
$url = 'http://localhost/'.$url;
print_r(parse_url($url));
?>

Comments

0
list($html, $get) = explode('?', $url);
$get = explode('=', $get);
print_r($get);

Comments

0
$parts = explode("=", $url);
$uid = $parts[1];

Comments

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.