开发者

Extracting a URL string with same results as a $_GET [duplicate]

开发者 https://www.devze.com 2023-04-13 09:07 出处:网络
This question already has answers here: Getting vars from URL (6 answers) Closed 9 years ago. Ok, so I have a string like so:
This question already has answers here: Getting vars from URL (6 answers) Closed 9 years ago.

Ok, so I have a string like so:

index.php?page=test2;sa=blah

And I need only to grab what gets returned using $_GET['page'], so it will return, test2 in this case. BUT page MU开发者_如何学PythonST be defined DIRECTLY after index.php? before it can return an actual string. How do I do this? I just need the same results returned from a $_GET for the page variable. I can also have just this:

index.php?page=blah

Should return blah

or this:

index.php?page=anything&sa=blah

Returns anything

or this:

index.php?action=blah;page=2

Returns empty string, cause this is NOT directly after index.php?

or basically any url, but it needs to grab the variable of page if it exists, only after index.php?. And sometimes page might not even exist at all, in this case, an empty string should be returned.

How can I do this? I am not browsing to the URL in my browser, so, don't think $_GET will work, but it needs to simulate and return the EXACT same results that $_GET will return in the browser, but only if page is defined directly after index.php?


parse_url and parse_str are your friends.


Please use following code:

function get($name, $url) {
   $src = parse_url($url);
   $src = $src['query'];
   parse_str($src, $tag);
   return $tag[$name];
}


you can check if the element received at $_GET is equal that you expect by comparison or a simple if statement. if not, then process the string to find it (if needed). Maybe also used the index of the elemnt to determine if process or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消