Better way to read last portion of URL

Posted by enloz on Stack Overflow See other posts from Stack Overflow or by enloz
Published on 2012-04-14T22:38:17Z Indexed on 2012/04/14 23:29 UTC
Read the original article Hit count: 196

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about string