maverick26 Posted May 24, 2006 Share Posted May 24, 2006 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. Link to comment https://forums.phpfreaks.com/topic/10376-parsing-a-url-string/ Share on other sites More sharing options...
maverick26 Posted May 24, 2006 Author Share Posted May 24, 2006 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'] Link to comment https://forums.phpfreaks.com/topic/10376-parsing-a-url-string/#findComment-38669 Share on other sites More sharing options...
maverick26 Posted May 24, 2006 Author Share Posted May 24, 2006 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]? Link to comment https://forums.phpfreaks.com/topic/10376-parsing-a-url-string/#findComment-38686 Share on other sites More sharing options...
kenrbnsn Posted May 24, 2006 Share Posted May 24, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10376-parsing-a-url-string/#findComment-38689 Share on other sites More sharing options...
maverick26 Posted May 24, 2006 Author Share Posted May 24, 2006 [!--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\" /] Link to comment https://forums.phpfreaks.com/topic/10376-parsing-a-url-string/#findComment-38693 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.