-
Parse URL
I need a url parse function for php 4.1+ I found the parse_url function but I need to parse the actual url the page is on and not just a constant string value or such.
Im trying to figure out if the page is the home page (index.php) because if its not then I need to do some conditional coding.
Thnaks
-
Re: Parse URL
parse_url($_SERVER['REQUEST_URI']);
-
Re: Parse URL
Thanks, and so with the actual url I can use some function to split the paths?
-
Re: Parse URL
parse_url will return an associative array containing the components of the URL. If you want, you can instead use basename() to get the file name of the URL or dirname() to get the directory.
If you want to check whether the current script is index.php, as opposed to the current URL, you can simply use:
PHP Code:
if (basename(__FILE__) == 'index.php') {
// ...
}
-
Re: Parse URL
Well the deal is that there may be multiple file names of the same names but different complete paths. So just comparing the file name is no good.
Seems like a combination of getting the basename and if its a root directory location would do the trick.
-
Re: Parse URL
So this will retrieve the current url into the variable. Then parse it and check the directory levels to make sure its at the root.
PHP Code:
$url = parse_url($_SERVER['REQUEST_URI']);
//Some kind of splitting to an array and then counting the ubound.
//Then match the file name?
arrUrl$ = somesplitfunction($url, '/');
if (basename(arrUrl[2]) == 'index.php') {
// ...
}
-
Re: Parse URL
I think this is it?
PHP Code:
$url = parse_url($_SERVER['REQUEST_URI']);
//Some kind of splitting to an array and then counting the ubound.
arrUrl$ = explode('/', $url);
//Check if its the root level
if (count(arrUrl$) == 3) {
//Then match the file name?
if (basename(arrUrl[3]) == 'index.php') {
//
}
}
-
Re: Parse URL
you need to have the dollar sign in front of your strings:
PHP Code:
$url = parse_url($_SERVER['REQUEST_URI']);
//Some kind of splitting to an array and then counting the ubound.
$arrUrl = explode('/', $url);
//Check if its the root level
if (count($arrUrl) == 3) {
//Then match the file name?
if (basename($arrUrl[3]) == 'index.php') {
//
}
}
-
Re: Parse URL
True, thanks. Just me being tired last night/morning and also being dyslexic. :D