Search code examples
phpstringurlpermalinks

Extract file name with PHP and make redirect URL


i have a file with name 2011-08-21-filename.php

I want to extract the date and the title, so the permalink will be something like /2011/08/21/filename


Solution

  • $filename = "2011-08-21-filename.php";
    preg_match('/(\d{4})-(\d{2})-(\d{2})-(\w*).php/', $filename, $matches);
    $path = implode("/", array_slice($matches, 1)); // => string(19) "2011/08/21/filename"