Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/10376-parsing-a-url-string/
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]
?
Link to comment
https://forums.phpfreaks.com/topic/10376-parsing-a-url-string/#findComment-38686
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\" /]
Link to comment
https://forums.phpfreaks.com/topic/10376-parsing-a-url-string/#findComment-38693
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.