2

On a page of mine, I have a GET as a URL of a website.

mypage.com/page.php?=URLHERE

On this URL, I need the ID at the very end of the URL

mypage.com/page.php?url=http://www.otherwebsite.com/something.php?id=%%%%%%%

These numbers are sometimes different amount of digits, so how would I do that?

My code:

  $url = $_GET['url'];

5 Answers 5

3

Assuming the url parameter is a properly encoded URL, then useparse_url() to get the URL components and parse_str() to retrieve the id parameter from its query string.

$url = $_GET['url'];

// First parse_url() breaks the original URL up
$parts = parse_url($url);

// parse_str() parses the query string from the embedded URL
$query = parse_str($parts['query']);

// Finally, you have your full id parameter
$id = $query['id'];
Sign up to request clarification or add additional context in comments.

Comments

0

assuming the the url has id at the begining of query_string

<?php
$url = $_GET['url'];
$url = basename($url);
$url =explode("?",$url);
$url = explode("=",$url[1]);
echo $url[1];
?>

Comments

0

try the parse_url

1 Comment

this answer would be much much improved by showing an example, but I think it is still a valid answer and not link-only.
0
$url = parse_url($_GET['url'], PHP_URL_QUERY);
$query = explode('=', $url);
$id = $query[1];

Comments

0

I'd use the PHP function explode() http://php.net/manual/en/function.explode.php

For this example

$numbers = explode("=",$url); 
$id_value = $numbers[2];
print $id_value;

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.