OhTheNoes Posted November 15, 2010 Share Posted November 15, 2010 Hello! Once again, I find myself in the need of help regarding PHP. What I have is a URL that looks like this: http://domain.com/gallery/?album=3&gallery=65 What I need is to extract the number that comes after "album=". In this case, it would be the number 3. I currently have this: <?php $url=getPageURL(); $var=explode('album=',$url); $var=explode('?',$var[1]); ?> My problem is that it doesn't result in anything. Can anyone lend a hand? It would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/ Share on other sites More sharing options...
Adam Posted November 15, 2010 Share Posted November 15, 2010 If this is something you'll be using several times, you may want to consider creating a function in combination with parse_url to return parameters in the query string. There's an example in a comment on the PHP manual.. Or if this is just a one-off case you could use preg_match: $url = 'http://domain.com/gallery/?album=3&gallery=65'; if (preg_match('/album\=([^&$]+)/', $url, $matches)) { echo $matches[1]; } Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134382 Share on other sites More sharing options...
OhTheNoes Posted November 15, 2010 Author Share Posted November 15, 2010 Hello! Thank you for taking the time to help me out. The code will be placed in a template file which will be called(?) each time someone opens a gallery page. To give a wider view, I'm trying to insert a "back to album" link in each gallery page. My original, incorrect code: <?php $url=getPageURL(); $var=explode('album=',$url); $var=explode('?',$var[1]); ?> <?php $albumVar=$var[1] ?> Back to <a href="http://domain.com/gallery/?album=<?php echo $albumVar ?>">gallery</a> Do you suggest I go with the function you mentioned or would the code you supplied be alright for this purpose? Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134383 Share on other sites More sharing options...
salathe Posted November 15, 2010 Share Posted November 15, 2010 You could also use parse_str to make sense of the query string. $url = 'http://domain.com/gallery/?album=3&gallery=65'; // album=3&gallery=65 $query_str = parse_url($url, PHP_URL_QUERY); // array('album' => '3', 'gallery' => '65'); parse_str($query_str, $query_arr); // outputs: 3 echo $query_arr['album']; That could be mushed on to one line, for you to echo as you want: <?php parse_str(parse_url(getPageURL(), PHP_URL_QUERY), $query_arr); ?> Back to <a href="http://domain.com/gallery/?album=<?php echo urlencode($query_arr['album']) ?>">gallery</a> Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134385 Share on other sites More sharing options...
OhTheNoes Posted November 15, 2010 Author Share Posted November 15, 2010 Fantastic. It works perfectly. You guys are the absolute best. Thanks very much salathe, MrAdam! Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134390 Share on other sites More sharing options...
trq Posted November 15, 2010 Share Posted November 15, 2010 Whats wrong with $_GET['album'] ? Or have I missed something completely? Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134394 Share on other sites More sharing options...
Adam Posted November 15, 2010 Share Posted November 15, 2010 I thought that at first Thorpe, but based on his original question it looked like he was dealing with a string. After the comment about getting back to the parent album though I'm not sure..? Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134434 Share on other sites More sharing options...
trq Posted November 15, 2010 Share Posted November 15, 2010 The subjects says "Getting variable from current URL". Maybe the OP should clarify. Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134670 Share on other sites More sharing options...
Adam Posted November 15, 2010 Share Posted November 15, 2010 Aha oh yer Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134720 Share on other sites More sharing options...
OhTheNoes Posted November 16, 2010 Author Share Posted November 16, 2010 The subjects says "Getting variable from current URL". Maybe the OP should clarify. Sorry! I am not very familiar with PHP terminology so I may have misrepresented some things with my words of choice. What can I do to help clarify the situation? Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134883 Share on other sites More sharing options...
Adam Posted November 16, 2010 Share Posted November 16, 2010 Are you wanting to return the "album" parameter from the URL the user is visiting, or from a URL contained within a variable? Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134897 Share on other sites More sharing options...
bob2006 Posted November 16, 2010 Share Posted November 16, 2010 would it be easier to wirte the code like this <?php $album=$_GET['album']; $gallery=$_GET['gallery']; echo"$album<br>$gallery"; if it not please forgive me for opening my big mouth Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134902 Share on other sites More sharing options...
Adam Posted November 16, 2010 Share Posted November 16, 2010 would it be easier to wirte the code like this <?php $album=$_GET['album']; $gallery=$_GET['gallery']; echo"$album<br>$gallery"; if it not please forgive me for opening my big mouth Think that was the point of Thorpe's post to be honest. Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134903 Share on other sites More sharing options...
plznty Posted November 16, 2010 Share Posted November 16, 2010 The subjects says "Getting variable from current URL". Maybe the OP should clarify. I was thinking exactly the same. haha. Use $_GET. Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134910 Share on other sites More sharing options...
OhTheNoes Posted November 16, 2010 Author Share Posted November 16, 2010 Are you wanting to return the "album" parameter from the URL the user is visiting, or from a URL contained within a variable? From the URL the user is visiting. I'm sorry if I made things look confusing What I meant from my title was how to get the album number which varies depending on the URL of the user. Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134915 Share on other sites More sharing options...
bob2006 Posted November 16, 2010 Share Posted November 16, 2010 that what i allways use works fine for me Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134916 Share on other sites More sharing options...
trq Posted November 16, 2010 Share Posted November 16, 2010 What I meant from my title was how to get the album number which varies depending on the URL of the user. That would be as simple as..... $album = $_GET['album']; Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134917 Share on other sites More sharing options...
OhTheNoes Posted November 16, 2010 Author Share Posted November 16, 2010 Oh I see! Would that work with everything in the format of x=y and you want to get y? Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134921 Share on other sites More sharing options...
trq Posted November 16, 2010 Share Posted November 16, 2010 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134923 Share on other sites More sharing options...
OhTheNoes Posted November 16, 2010 Author Share Posted November 16, 2010 Ah great. Fantastic. Thank you all for your help, not just with this problem, as I'm sure this will definitely help me a lot in the future. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/#findComment-1134924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.