wploh67 Posted March 20, 2008 Share Posted March 20, 2008 How do i extract the domain name and query string (id=12345) from a given URL? For example, http://www.bookshop.com/book/en/catalog.php?id=12345 Thanks. Regards, WP Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/ Share on other sites More sharing options...
huhn_m Posted March 20, 2008 Share Posted March 20, 2008 the query string you get with the variables $_REQUEST[$what] where what is the variable to request (here "id") Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/#findComment-496626 Share on other sites More sharing options...
uniflare Posted March 20, 2008 Share Posted March 20, 2008 barand gave me this answer after i gave him a preg_match expression that did the same. Quote from Barand: <?php $a = parse_url('http://sub.domain.tld:8080/foldera/folderb/index.php?request=value'); echo '<pre>', print_r($a, true), '</pre>'; ?> --- just for ref my preg_match is: its a regex with preg_match that will store all parts of a domain name into an array, eg: http://sub.domain.tld:8080/foldera/folderb/index.php?request=value will return: Array( [ 0] => "http" [ 1] => "sub.domain.tld" [ 2] => "8080" [ 3] => "/foldera/folderb/" [ 4] => "index.php" [ 5] => "?request=value" ) ----------- Now the function that does this: (The above noted output is stored in $matches2 variable) preg_match('/\A(http:\/\/|https:\/\/)*([a-zA-Z0-9_\-\.]+):*([0-9]+)*\/*([a-zA-Z0-9_\-\.]+\/)*(.*\.\w*)*(\?.*)*/i',$url,$matches2); hope this helps, Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/#findComment-496628 Share on other sites More sharing options...
kenrbnsn Posted March 20, 2008 Share Posted March 20, 2008 There is a builtin function that does that: parse_url(). <?php $url = 'http://www.bookshop.com/book/en/catalog.php?id=12345'; $pu = parse_url($url); echo '<pre>' . print_r($pu,true) . '</pre>; ?> Will give you Array ( [scheme] => http [host] => www.bookshop.com [path] => /book/en/catalog.php [query] => id=12345 ) Ken Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/#findComment-496643 Share on other sites More sharing options...
uniflare Posted March 20, 2008 Share Posted March 20, 2008 kenrbnsn i know thats what i said, look up Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/#findComment-496645 Share on other sites More sharing options...
kenrbnsn Posted March 20, 2008 Share Posted March 20, 2008 Sorry, I just saw your preg_match answer and didn't look at the quote that carefully. Ken Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/#findComment-496690 Share on other sites More sharing options...
alexloh Posted March 20, 2008 Share Posted March 20, 2008 Yes, you can use PHP's parse_url() function to accomplish that. And in case if you want to retrieve just either the domain name or query string, you can pass in an optional component parameter to the parse_url() function. It will then return a string instead of an array. <?php $url = 'http://www.bookshop.com/book/en/catalog.php?id=12345'; // to return just the domain name echo parse_url($url, PHP_URL_HOST); ?> Details see code example at: http://www.thewebscripter.com/tutorial/code_examples/parse_url.php Hope this helps. Alex Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/#findComment-496810 Share on other sites More sharing options...
Styles2304 Posted March 20, 2008 Share Posted March 20, 2008 couldn't you also just do $_GET['id']? Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/#findComment-496815 Share on other sites More sharing options...
kenrbnsn Posted March 20, 2008 Share Posted March 20, 2008 No, the OP asked how to retrieve information from a given URL, not how to get the value passed via the URL that invoked the script. These are two different actions. Ken Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/#findComment-496846 Share on other sites More sharing options...
wploh67 Posted March 21, 2008 Author Share Posted March 21, 2008 Thanks everyone for your guides & tips. You guys are helpful. Thanks. Regards, WP Quote Link to comment https://forums.phpfreaks.com/topic/97055-extract-data-from-url/#findComment-497299 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.