Forum Moderators: coopster

Message Too Old, No Replies

Parsing an Url

parse_url doesn't seem to be what I'm looking for.

         

otem

5:28 am on Feb 9, 2007 (gmt 0)

10+ Year Member



Let's say I have a variable that looks like this:

$url="http://www.example.tld/index.php?var1=#*$!&something=yyy&setting=zzz";

If I wanted to parse this url to grab all the variables, what could I use?

It would seem I would want to use parse_url, but that looks like it will take all the parts that I do want and just throws them into $url[query].

Any ideas?

Thanks.

whoisgregg

6:14 am on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do the parse_url first, then split apart the query:

$pairs = [url=http://php.net/explode]explode[/url]('&', $url['query']);
foreach($pairs as $pair){
$pair_split = explode('=', $pair, 1);
$array[$pair_split[0]] = $pair_split[1];
}
print_r($array);

Untested pseudocode, but it should be close. :)

otem

6:16 pm on Feb 9, 2007 (gmt 0)

10+ Year Member



Thanks so much WIG,

I would definitely want a loop approach for this task right, because I'll be chopping up the data into smaller separate bits?

coopster

10:54 pm on Feb 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Depending on your intentions, parse_str [php.net] may be an option as well.

otem

2:04 am on Feb 21, 2007 (gmt 0)

10+ Year Member



Thank you,

parse_str works perfectly for what I need.