Jump to content

Strip data from URL parameter


warrenk

Recommended Posts

I am trying to strip data from a URL parameter into a variable.

example:
www.website.com/product.php?id=redwidgit--1
www.website.com/product.php?id=bluewidgit--2
www.website.com/product.php?id=greenwidgit--3

What I am trying to extract is the number at the end of the parameter (ie. 1,2,3).  The number is always at the end of the parameter and it is after two dashes ('--').

Any help greatly appreciated!

Thanks,
Warren
Link to comment
https://forums.phpfreaks.com/topic/35429-strip-data-from-url-parameter/
Share on other sites

There has GOT to be a better way to do this - perhaps just get the whole, unmolested query string (I don't know how), but here is [i]a[/i] solution...

[code]
function getValue(){
print_r($_GET);
$returnMe = false;

$count = sizeof($_GET); //How many items are in the query string
foreach($_GET as $variable => $value){
$count--; //subtract 1
if($count == 0){ //Are we at the last key in the array?
$holder = explode('--',$value);
$holder_size = sizeof($holder);
if($holder_size > 1)
$returnMe = $holder[$holder_size - 1];
else
$returnMe = false;
}
}

return $returnMe;
}

echo "value is ".getValue();
[/code]
deltran, I think it will always be id, so no need to go through the array.
[code]
$id = $_GET['id'];
$spot = strrpos($id, '-'); //use strrpos, not strpos
$num = intval(substr($id, $spot, strlen($id)));[/code]

Note: I didn't test this, but I think it will work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.