NotionCommotion Posted January 13, 2015 Share Posted January 13, 2015 I have a string that looks like /index.php?g1=111&g2=222&g3=333. How can I obtain the value of g1 (i.e. 111)? It does not represent the current state of the server thus I cannot just use $_GET. It also is not necessarily the first item.The script below appears to work, however, http://php.net/manual/en/function.parse-url.php states This function is intended specifically for the purpose of parsing URLs and not URIs. However, to comply with PHP's backwards compatibility requirements it makes an exception for the file:// scheme where triple slashes (file:///...) are allowed. For any other scheme this is invalid. It appears that my string is a URI and not a URL, but I might be wrong.How should this be accomplished <?php $str='/index.php?g1=111&g2=222&g3=333'; $array=parse_url($str); parse_str($array['query'],$get); echo("<p>{$get['g1']}</p>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/293895-extracting-get-variables-from-a-string/ Share on other sites More sharing options...
Ch0cu3r Posted January 13, 2015 Share Posted January 13, 2015 (edited) First use substr to get the query string (whatever is after the ?). Then pass the query string to parse_str $str='/index.php?g1=111&g2=222&g3=333'; $query_string = substr($str, strpos($str, '?')+1); parse_str($query_string, $array); printf('<pre>%s</pre>', print_r($array, 1)); Edited January 13, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/293895-extracting-get-variables-from-a-string/#findComment-1502822 Share on other sites More sharing options...
NotionCommotion Posted January 13, 2015 Author Share Posted January 13, 2015 First use substr to get the query string (whatever is after the ?). Then pass the query string to parse_str Thanks. I like your approach more. Quote Link to comment https://forums.phpfreaks.com/topic/293895-extracting-get-variables-from-a-string/#findComment-1502831 Share on other sites More sharing options...
Barand Posted January 13, 2015 Share Posted January 13, 2015 or <?php $str = "/index.php?g1=111&g2=222&g3=333"; $bits = parse_url($str); parse_str($bits['query'], $vals); echo '<pre>',print_r($vals, true),'</pre>'; ?> gives Array ( [g1] => 111 [g2] => 222 [g3] => 333 ) Quote Link to comment https://forums.phpfreaks.com/topic/293895-extracting-get-variables-from-a-string/#findComment-1502832 Share on other sites More sharing options...
NotionCommotion Posted January 13, 2015 Author Share Posted January 13, 2015 Hi Barand, Your code is what I posted in my original post. Quote Link to comment https://forums.phpfreaks.com/topic/293895-extracting-get-variables-from-a-string/#findComment-1502840 Share on other sites More sharing options...
Barand Posted January 13, 2015 Share Posted January 13, 2015 Doh! So it is. As it works, what was your problem then? Quote Link to comment https://forums.phpfreaks.com/topic/293895-extracting-get-variables-from-a-string/#findComment-1502844 Share on other sites More sharing options...
NotionCommotion Posted January 14, 2015 Author Share Posted January 14, 2015 deja vu! A couple of posts were deleted by someone, and where almost identical. Quote Link to comment https://forums.phpfreaks.com/topic/293895-extracting-get-variables-from-a-string/#findComment-1502874 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.