Reputation: 5
This is a two part question. First how can I grab the last url value from a link when I dont know how deep the value is, for example how can I grab
the last value of sub_4
from the link example below using PHP? And second how can I grab the url value cat=3
and the last url value sub_4
using PHP?
I'm kind of new to PHP so a detailed step by step example would help me out a lot if its not too much trouble.
Thanks in advance!
Here is an example of a URL value.
http://www.example.com/categories/index.php?cat=3&sub_1=sub1&sub_2=sub2&sub_3=sub3&sub_4=sub4
Upvotes: 0
Views: 509
Reputation: 2368
This example is under the assumption you want to find the very first variable in the GET query, and the very last:
<?php
// Here, you separate all the variables
$parameters = explode("&", $_SERVER['QUERY_STRING']);
// Get the last key's index
$last_key_index = count($parameters)-1;
// Easy way for PHP 5.3
$first_key = strstr($parameters[0], "=", true);
$last_key = strstr($parameters[$last_key_index], "=", true);
// Bit longer otherwise
$first_key = substr($parameters[0], 0, strpos($parameters[0], "="));
$last_key = substr($parameters[$last_key_index], 0, strpos($parameters[$last_key_index], "="));
// Test it here
echo($first_key);
echo($last_key);
?>
Upvotes: 0
Reputation: 11813
Query parameters (anything in the URL with ?name
or &name
) are saved in the $_GET
superglobal.
If sub
is a hierarchy, you should probably just write it as such. For instance: ?sub=path/to/sub
or ?sub=path:to:sub
From this you can explode()
on your separator (/
or :
) to get the different parts of the sub
parameter. $sub_array = explode('/', $_GET['sub']);
You can then iterate over the array using a foreach or directly access the highest branch of the hierarchy using count():
$sub_array[count($sub_array-1)];
If you have an array of subs that you want to use to generate a URL, you can use implode()
to build your URL params. $sub_params = implode('/', $_GET['sub']);
You might construct that array by appending each sub
to the $sub_array
.
$sub_array[] = 'sub1';
$sub_array[] = 'sub2';
$sub_array[] = 'sub3';
etc.
If you get lost, use var_dump($_GET)
or var_dump($variable)
to see what's inside it.
Upvotes: 1
Reputation: 11574
All variables from the hook will be returned on $_GET. So if you want to get that value from the URL, just use:
$_GET['sub_4']
If you want to get a list of all of the possible values:
array_keys($_GET)
This will give you all of the variables.
UPDATE: I just reread your question. I think I better understand what you are looking for. Correct me if I am wrong, but you are not certain how to get the last element of the string? Is that right? So you could potentially have sub_5, sub_6, sub_7 etc. Here is how to get the last element from the string:
end($_GET);
$key = key($_GET);
$last_item = $_GET[$key];
$last_item
will now have sub_4 (or what ever the last item is).
Upvotes: 1
Reputation: 93177
For the first part; you should use the $_GET[] array which contain every parameter passed in the url $_GET['cat']
, $_GET['sub_1']
.
For the second part, in your case you should try send an array in parameter like this :
http://www.example.com/categories/index.php?cat=3&sub[1]=sub1&sub[2]=sub2&sub[3]=sub3&sub[4]=sub4
And then use the $_GET['sub'][]
array $_GET['sub'][1]
, $_GET['sub'][2]
, ...
Now you can determine the length of the array and know the last item in it.
Upvotes: 0
Reputation: 21388
Each cat=3
or sub_1
value can be retreived by $_GET['cat']
and $_GET['sub_1']
respectively. To elaborate, $_GET['NAME_OF_PROPERTY']
would look like php.net/index.php?NAME_OF_PROPERTY=whatever
It seems like both questions were on how to grab the value from the URL, so I hope that answers both.
In your example url http://www.example.com/categories/index.php?cat=3&sub_1=sub1&sub_2=sub2&sub_3=sub3&sub_4=sub4
echo $_GET['cat']; // 3
echo $_GET['sub_1']; // sub1
echo $_GET['sub_2']; // sub2
echo $_GET['sub_3']; // sub3
echo $_GET['sub_4']; // sub4
Upvotes: 0