0

I know you can easily get a parameter from the page you're currently on, but can you easily do the same from any URL string?

I need to grab the "id" parameter out of a string like https://market.android.com/details?id=com.zeptolab.ctr.paid?t=W251bGwsMSwxLDIxMiwiY29tLnplcHRvbGFiLmN0ci5wYWlkIl0.

How can I do this with PHP? Is there a built-in function, or do I have to use regex?

4 Answers 4

4

You could use combination of parse_url and parse_str:

$url = 'https://market.android.com/details?id=com.zeptolab.ctr.paid?t=W251bGwsMSwxLDIxMiwiY29tLnplcHRvbGFiLmN0ci5wYWlkIl0';
$arr = parse_url($url);
parse_str($arr['query']);
echo $id; //com.zeptolab.ctr.paid?t=W251bGwsMSwxLDIxMiwiY29tLnplcHRvbGFiLmN0ci5wYWlkIl0
Sign up to request clarification or add additional context in comments.

Comments

2

Yes you can.

parse_url()

From the PHP docs:

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>

The above example will output:

Array
(
    [scheme] => http
    [host] => hostname
        [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

/path

Comments

2

There's parse_url():

function extractGETParams($url)
{
  $query_str = parse_url($url, PHP_URL_QUERY);
  $parts = explode('&', $query_str);
  $return = array();

  foreach ( $parts as $part )
  {
    $param = explode('=', $part);
    $return[$param[0]] = $param[1];
  }

  return $return;
}

$url = 'http://username:password@hostname/path?arg=value&arg2=value2#anchor';

var_dump( extractGETParams($url) );

On Codepad.org: http://codepad.org/mHXnOYlc

Comments

0

You can use parse_str(), and then access the variable $id.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.