warrenk Posted January 23, 2007 Share Posted January 23, 2007 I am trying to strip data from a URL parameter into a variable.example:www.website.com/product.php?id=redwidgit--1www.website.com/product.php?id=bluewidgit--2www.website.com/product.php?id=greenwidgit--3What 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 Quote Link to comment https://forums.phpfreaks.com/topic/35429-strip-data-from-url-parameter/ Share on other sites More sharing options...
Deltran Posted January 23, 2007 Share Posted January 23, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/35429-strip-data-from-url-parameter/#findComment-167615 Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/35429-strip-data-from-url-parameter/#findComment-167619 Share on other sites More sharing options...
warrenk Posted January 23, 2007 Author Share Posted January 23, 2007 Thank you both for your quick replies! Jesirose, you are correct, it is always 'id'. The code is exactly what I was looking for. Appreciate your help!Warren Quote Link to comment https://forums.phpfreaks.com/topic/35429-strip-data-from-url-parameter/#findComment-167634 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.