Jump to content

Getting variable from current URL


OhTheNoes

Recommended Posts

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.

Link to comment
Share on other sites

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];
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
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.