Page 1 of 1

Parsing a URL

Posted: Tue Apr 12, 2005 7:19 pm
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

Re: Parsing a URL

Posted: Tue Apr 12, 2005 7:31 pm
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 ;-)

Posted: Tue Apr 12, 2005 8:22 pm
by sulen
Yeah. That works thank you very much for the quick repsonse. Thanks

Posted: Thu Apr 14, 2005 5:57 pm
by feyd
*clears throat*

parse_url(), parse_str(). :)