Jump to content


Photo

Parsing a URL string


  • Please log in to reply
4 replies to this topic

#1 maverick26

maverick26
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 24 May 2006 - 05:56 PM

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:
http://mydomain.com/forums/index.php?showtopic=7383&pid=59790
And I want to extract and store the 7383 in a variable such as $topicid. I may also have an input such as:
http://guardianhq.com/forums/index.php?showtopic=7383
where I want the same thing returned. How would I go about this?

Thanks in advance.

#2 maverick26

maverick26
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 24 May 2006 - 06:26 PM

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']

#3 maverick26

maverick26
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 24 May 2006 - 07:31 PM

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:

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);
}
So, now my question is, will that code set $topicid to 7383 if:
$referurl="http://mydomain.com/forums/index.php?showtopic=7383&pid=59790"
or
$referurl="http://mydomain.com/forums/index.php?showtopic=7383"
?


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 May 2006 - 07:49 PM

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

#5 maverick26

maverick26
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 24 May 2006 - 08:16 PM

[!--quoteo(post=376792:date=May 24 2006, 03:49 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 24 2006, 03:49 PM) View Post[/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\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users