Hey Everyone, How can I use PHP to get parts out of an URL? Example: http://www.website.com/first-part/second-part/whatever/ I want to extract the first-part and second-part in the url and echo them. I know there is something with dir_name but I can't figure it out for the life of me. Thanks!
Whether or not you can do this will depend on accessibility. If access to the first-part is restricted, you won't be able to view or extract it. Many websites, for instance, have their images directory restricted so that while you may be able to view an image file in that directory, you can't view the complete contents of that directory.
Hey Jameyson, The folders don't really exist, they are created in .htaccess, I just need to get the text parts out, there is no accessibility issues.
Disregard that last answer, it's completely off the mark. What you can do, if you always know which parts you want to extract, just check out this: http://php.net/manual/en/function.parse-url.php and do an explode() on the path item returned
Thanks PopSiCLe! For anyone else looking here is the code: $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $path = parse_url($url, PHP_URL_PATH); $segments = explode('/', rtrim($path, '/')); PHP: Than you can just echo the outputs for as many as you have like so: <?php echo $segments[1]; ?> <?php echo $segments[2]; ?> ETC Code (markup):