Code: Select all
<?php
function page_name() { //Should be named pageName!
$uri = $_SERVER['REQUEST_URI']; //Get everything after the extension, ie. example.com/test.php will return /test.php
$uri = substr($uri, 1); //Cut off the first /
$firstSplit = explode('/', $uri); //Split directories up
$dirCount = count($firstSplit) - 1; //Get the final part, our file, and remove one as arrays start at 0.
$pageName = explode('.', $firstSplit[$dirCount]); //Remove our extension
return $pageName[0];
}
?>
If you had PHP 5.3, you could use strstr's new third param which may be quicker than exploding... but the above will do what you require.
If your pages don't have an extension, i.e.
http://www.example.com/this-is-my-page/, then the above won't work. If so, let me know and I'll write a revised copy.