Parsing a URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Parsing a URL

Post by sulen »

I have a variable degreetypeid which holds a URL like

$degreetypeid="start3.php?degreetypeid=1&degreetype=Associates"

I need to parse this URL to get the value of degreetypeid in the URL which in the above case is 1. The code I have used is as under

Code: Select all

<? preg_match("/[degreetypeid]\s*=\s*(.*?)/i", $degreetypeid, $matches);
$degree = $matches[1];
echo $degree;
?>
But it doesnt seem to work for some reason. Can anyone tell me what the mistake is. Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Parsing a URL

Post by Chris Corbyn »

sulen wrote:I have a variable degreetypeid which holds a URL like

$degreetypeid="start3.php?degreetypeid=1&degreetype=Associates"

I need to parse this URL to get the value of degreetypeid in the URL which in the above case is 1. The code I have used is as under

Code: Select all

<? preg_match("/[degreetypeid]\s*=\s*(.*?)/i", $degreetypeid, $matches);
$degree = $matches[1];
echo $degree;
?>
But it doesnt seem to work for some reason. Can anyone tell me what the mistake is. Thanks
[abc] means match any SINGLE character 'a', 'b', or 'c' so your [degreetypeid] is only a single char

do this ;-) (Ignore my escaping \= I always do it for some reason).

Code: Select all

preg_match("/\?degreetypeid\s*\=\s*([^\&]*)/i", $degreetypeid, $matches);
echo $matches[1];
If it needs to be more specific then ask me ;-)
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

Yeah. That works thank you very much for the quick repsonse. Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

*clears throat*

parse_url(), parse_str(). :)
Post Reply