Better way to read last portion of URL
- by enloz
This script should detect the last portion in the full path, and if it is stackoverflow output ok
$current_url = $_SERVER['REQUEST_URI'];
$current_url_arr = explode('/',$current_url);
$count = count($current_url_arr);
if($current_url_arr[$count-2] == 'stackoverflow'){
echo 'ok';
}
else {
echo 'not ok';
}
Example 1: www.myserver.ext/something/else/stackoverflow/
Output: ok
Example 2: www.myserver.ext/something/else/stackoverflow
Output: not ok
Example 3: www.myserver.ext/something/else/stackoverflow/foo
Output: not ok
I hope that you understand the idea. This script works fine, but I'm wondering if there is any better, elegant way to read last portion of URL?