Php get url path after domain
I am a complete beginner to PHP, so please keep that in mind. I am attempting to get JUST the path from the current URL, which is variable. For instance, if the current URL is This is the PHP code I have so far:
However, this does not work, and I can't seem to Google a suitable solution. Does anyone have any pointers? Thanks in advance! asked Oct 17, 2014 at 16:09
1 You can use
This will give you exactly what you wanted ( answered Oct 17, 2014 at 16:16
Ryan WillisRyan Willis 6063 silver badges13 bronze badges 0 The following should give you "/blog/2014/" from your example:
answered Oct 17, 2014 at 16:14
danmullendanmullen 2,5603 gold badges19 silver badges28 bronze badges 2 you can do this:
answered Oct 17, 2014 at 16:18
Peyman.HPeyman.H 1,8011 gold badge16 silver badges26 bronze badges 0 Home / Extract domain, path etc from a full url with PHP PHP’s parse_url function makes it easy to extract the domain, path and other useful bits of information from a full URL. This can be useful for a variety of purposes, such as when spidering content from a website and needing to extract particular pieces of information from the links in the page. Returning an associative arrayThe parse_url function takes the url as the first argument and an optional component value as the second argument. If the second argument is omitted then it returns the found values as an associative array. This post is at https://www.electrictoolbox.com/php-extract-domain-from-full-url/ and getting the associative array of information from it is done like this: $url = "https://www.electrictoolbox.com/php-extract-domain-from-full-url/"; $parts = parse_url($url); Then doing print_r($parts) will output this: Array ( [scheme] => http [host] => www.electrictoolbox.com [path] => /php-extract-domain-from-full-url/ ) If they are present, the array will also contain values for port, user, pass (i.e. password), query (the query string component of the URL) and fragment (the part after the #). Returning a stringIf all you are after is a single component from the array as a single string, pass the second "component" parameter from the following constants: PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT. To just get the domain from this blog post’s URL, do this: $url = "https://www.electrictoolbox.com/php-extract-domain-from-full-url/"; $domain = parse_url($url, PHP_URL_HOST); PHP DocumentationFor more information read the PHP manual page for this function. Follow up postsHave a read of my post titled "PHP: get keywords from search engine referer url" to find out how to use the parse_url function in conjunction with the parse_str function to see what query string visitors have entered into a search engine. |