Search code examples
phpbreadcrumbsstrpos

trim leading forward slash and .php off file name


The code below gives me the file name of the page I'm on e.g. '/index.php' I want to know how I can trim this to be just index without the / and the .php.

$pageName = $_SERVER['SCRIPT_NAME'];
echo $pageName;

I'm using this to output the location of where I am for a simple site breadcrumb.

Thanks in advance.


Solution

  • Use pathinfo (requires php 5.2+):

    $pageName = $_SERVER['SCRIPT_NAME'];
    echo pathinfo($pageName, PATHINFO_FILENAME);
    

    Alternatively, for a less flexible solution, use php's string functions and basename:

    echo substr(basename($pageName), 0, -strlen('.php'));