Webmozart\PathUtil\Path::getFilenameWithoutExtension PHP Method

getFilenameWithoutExtension() public static method

Returns the file name without the extension from a file path.
Since: 1.1 Added method.
Since: 2.0 Method now fails if $path or $extension have invalid types.
public static getFilenameWithoutExtension ( string $path, string | null $extension = null ) : string
$path string The path string.
$extension string | null If specified, only that extension is cut off (may contain leading dot).
return string The file name without extension.
    public static function getFilenameWithoutExtension($path, $extension = null)
    {
        if ('' === $path) {
            return '';
        }
        Assert::string($path, 'The path must be a string. Got: %s');
        Assert::nullOrString($extension, 'The extension must be a string or null. Got: %s');
        if (null !== $extension) {
            // remove extension and trailing dot
            return rtrim(basename($path, $extension), '.');
        }
        return pathinfo($path, PATHINFO_FILENAME);
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage The extension must be a string or null. Got: array
  */
 public function testGetFilenameWithoutExtensionFailsIfInvalidExtension()
 {
     Path::getFilenameWithoutExtension('/style.css', array());
 }