Skip navigation.
Home

Automatically Naming a Page From the File Name

This bit of code will set the page's title based on the filename of the script. So, my_homepage.php will be titled "My Homepage". Laziness prevails, sort of.

<?php
    $title = $_SERVER['SCRIPT_NAME'];
    $title = ltrim($title,'/');
    $title = preg_replace('/\.php$/','',$title);
    if ( strpos($title,'_') === FALSE )  // if we don't have a _ in there, it's a single word
    {
        $title = ucfirst($title);
    }
    else
    {
        $title = explode('_',$title);
        $title = array_map(myucfirst,$title);
        $title = join( ' ', $title);
    }
    print "<title>$title</title>";

    function myucfirst($s) { return ucfirst($s); }
?>