Jump to content

Archived

This topic is now archived and is closed to further replies.

maverick26

Parsing a URL string

Recommended Posts

Ok, so here is what I want to accomplish; I have a string input that is a url which I want to extract the number following the text "showtopic=" and may or may not have text following it, but if it does, it will always be a "&". so for example, I might recieve the following as input:
[code]http://mydomain.com/forums/index.php?showtopic=7383&pid=59790[/code]
And I want to extract and store the 7383 in a variable such as $topicid. I may also have an input such as:
[code]
http://guardianhq.com/forums/index.php?showtopic=7383[/code]
where I want the same thing returned. How would I go about this?

Thanks in advance.

Share this post


Link to post
Share on other sites
Oh, and I just realized you may be confused by my request.. I am recieving this url string as input from HTTP_REFER, in a php script that is not "index.php" so I can't just retreive the value of showtopic via $_GET['showtopic']

Share this post


Link to post
Share on other sites
Alright well I tried solving it by myself, and this is what I came up with.

NOTE: I realized that the number I want will always be after the first equals sign (=) and will either encompass the rest of the string or end at the first ampersand (&), so I used those chars instead:

[code]if (strpos($referurl, '='))
  $startpos = (strpos($referurl, '=')) + 1;
if (strpos($referurl, '&')) {
  $endpos = strpos($referurl, '&');
  $length = $endpos - $startpos;
  $topicid = substr($referurl, $starpos, $length);
} else {
  $topicid = substr($referurl, $startpos);
}[/code]
So, now my question is, will that code set $topicid to 7383 if:
[code]$referurl="http://mydomain.com/forums/index.php?showtopic=7383&pid=59790"[/code]
or
[code]$referurl="http://mydomain.com/forums/index.php?showtopic=7383"[/code]
?

Share this post


Link to post
Share on other sites
Save yourself the trouble of doing yourself and take a look at the functions [a href=\"http://www.php.net/parse_url\" target=\"_blank\"]parse_url()[/a] and [a href=\"http://www.php.net/parse_str\" target=\"_blank\"]parse_str()[/a].

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=376792:date=May 24 2006, 03:49 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 24 2006, 03:49 PM) [snapback]376792[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Save yourself the trouble of doing yourself and take a look at the functions [a href=\"http://www.php.net/parse_url\" target=\"_blank\"]parse_url()[/a] and [a href=\"http://www.php.net/parse_str\" target=\"_blank\"]parse_str()[/a].

Ken
[/quote]
Oh wow, that is soo much easier, Thanks Ken! [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

For anyone that cares, here is the revised code using those two functions..
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$urlarray = parse_url($referurl);
parse_str($urlarray[query]);
$topicid = $showtopic;[/quote]
[img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.