php_begins Posted November 28, 2011 Share Posted November 28, 2011 i have had a difficult time trying to work this out.I need to do some pattern matching for certain urls and retrieve information from it. For example, $url=http://www.test.com/showpic.php?do=showpic&u=89165&a=34933 if $url contains the value showpic.php,then i need to retrieve the following { $u=value of u(i.e. 89165 in this case) $a=value of a(i.e. 34933 in this case) } else do nothin.. the format of the url will always be the same as above if it contains showpic.php Quote Link to comment https://forums.phpfreaks.com/topic/251989-retrieving-information-from-url/ Share on other sites More sharing options...
requinix Posted November 28, 2011 Share Posted November 28, 2011 parse_url and parse_str should be useful... Quote Link to comment https://forums.phpfreaks.com/topic/251989-retrieving-information-from-url/#findComment-1291997 Share on other sites More sharing options...
php_begins Posted November 28, 2011 Author Share Posted November 28, 2011 so far here is what i did.. i dont know how to check if the url contains showpic.php with parse_url yet. so $purl=http://www.test.com/showpic.php?do=showpic&u=89165&a=34933; parse_str($purl); echo $u."<br>"; echo $a."<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/251989-retrieving-information-from-url/#findComment-1292004 Share on other sites More sharing options...
requinix Posted November 28, 2011 Share Posted November 28, 2011 parse_url() comes first. You'll get a path - check if it is for showpic.php (using substr() or better basename). If so, use parse_str() on the query string. Quote Link to comment https://forums.phpfreaks.com/topic/251989-retrieving-information-from-url/#findComment-1292006 Share on other sites More sharing options...
QuickOldCar Posted November 28, 2011 Share Posted November 28, 2011 Here's just something simple I did using preg_match to see if showpic.php exists in the url, and if does find the u and a values. <?php $url = "http://www.test.com/showpic.php?do=showpic&u=89165&a=34933"; if (preg_match("/showpic.php/i",$url)) { echo "yes <br />"; parse_str($url, $values); $a_value = $values['a']; $u_value = $values['u']; echo "a is $a_value <br />"; echo "u is $u_value <br />"; } else { echo "not showpic.php"; } ?> result is: yes a is 34933 u is 89165 Quote Link to comment https://forums.phpfreaks.com/topic/251989-retrieving-information-from-url/#findComment-1292014 Share on other sites More sharing options...
php_begins Posted November 29, 2011 Author Share Posted November 29, 2011 thank u @requinix and @quickoldcar..both the recomendations work great.. Quote Link to comment https://forums.phpfreaks.com/topic/251989-retrieving-information-from-url/#findComment-1292210 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.