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
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]
Link to comment
Share on other sites

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