PDA

View Full Version : fetching part from url


docock
11-13-2008, 02:38 PM
I use the following line to retrieve part of the url:

$parameter=$_SERVER["REQUEST_URI"];

Let's say the url = 'http:\\www.justanexample.com\test\index.html?1234

this gives me: test\index.html?1234 as result

However I only want to retrieve the information that's behind the question mark, in this case 1234.

how can I alter the coding so it only retrieve whats behind the question mark?

194673
11-13-2008, 02:46 PM
$parameter=$_SERVER["REQUEST_URI"];
$pieces = explode("?",$parameter);
$param = $pieces[1];

Zangeel
11-13-2008, 04:15 PM
<?php
$PARA = "http:\\www.justanexample.com\test\index.html?1234";
$get = strstr($PARA, '?');
$get = str_replace('?', ' ',$get);
echo $get;
?>


help?

tosbourn
11-13-2008, 04:25 PM
Out of interest, what is the better way of doing this?

My first thought was to use explode but I guess arrays that just that little bit longer to access than a normal string.

194673
11-13-2008, 05:08 PM
Well if you want to use strings then I would have done it this way:

$parameter=$_SERVER["REQUEST_URI"];
$dl = "?";
$parsed = substr($parameter,strpos($parameter,$dl),strlen($parameter)+1);

tosbourn
11-13-2008, 05:53 PM
Personally I would stick with arrays, I was just thinking out loud what is the "best" solution.

bazz
11-13-2008, 06:07 PM
I have always known that what's after the question mark is called the 'query string'. Isn'tthere a way, in php, of grabbing that on its own without the url?

in perl its like

my $query_string = $ENV{"QUERY_STRING"};

then you would split it on the delimiter, which could be either one of '&' OR 'amp;' (without the single quotes).

Having googled a bit, I found this link (http://discomoose.org/2005/10/19/how-to-use-the-query-string-in-php/)

bazz

194673
11-13-2008, 06:52 PM
Well, I was confused by the fact that after the ?, there was the numbers "1234". I'm not sure if the example was just poorly worded, or if that was actually supposed to be like that. There is not $_GET variable there, just a string after the ?.

bazz
11-13-2008, 07:55 PM
Ahh the dreaded speed-reading again. :(

I missed that.

bazz

kuantz
11-13-2008, 08:26 PM
I think the fast and simple way is with:

list($before, $after) = split('\?', $your_string, 2);

--------------
I used in some jobs at best online casino (http://casinotop100list.com/best-online-casino/) and top online poker (http://casinotop100list.com/_Top+Online+Poker/).

Fou-Lu
11-14-2008, 03:00 AM
Eh? Why do the work?
This should all be in the QUERY_STRING of the $_SERVER superglobal. Split it as necessary or perform you're lookups via $_GET. Its not very often you end up with a query string that isn't split into key=>value pairs though, but best I know it will still be within the QUERY_STRING value.

hip_hop_x
11-14-2008, 11:11 AM
well if the $_GET doesn't work to you, then here's another way with regex

$url="http:\\www.justanexample.com\test\index.html?1234";

preg_match("#?(\d+)#",$url,$match);
var_dump($match);

abduraooft
11-14-2008, 11:27 AM
Eh? Why do the work?
OP's url is just
http:\\www.justanexample.com\test\index.html?1234

@docock : Is your html page is capable to execute PHP script? Or do you have any RewriteRules ?

Fou-Lu
11-14-2008, 08:13 PM
OP's url is just
http:\\www.justanexample.com\test\index.html?1234

@docock : Is your html page is capable to execute PHP script? Or do you have any RewriteRules ?

Yes, then $_SERVER['QUERY_STRING'] is what you want to use. On the provided url it will return '1234'.
If its not from an incoming url, use parse_url($sURL, PHP_URL_QUERY).

Being that $_SERVER['REQUEST_URI'] provides him with test\index.html?1234 says that html is set up to be parsed as a PHP page, which means that $_SERVER['QUERY_STRING'] should also work.