Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Path.php

function Path::changeExtension

Changes the extension of a path string.

Parameters

string $path The path string with filename.ext to change.:

string $extension new extension (with or without leading dot):

Return value

string the path string with new file extension

File

vendor/symfony/filesystem/Path.php, line 339

Class

Path
Contains utility methods for handling path strings.

Namespace

Symfony\Component\Filesystem

Code

public static function changeExtension(string $path, string $extension) : string {
    if ('' === $path) {
        return '';
    }
    $actualExtension = self::getExtension($path);
    $extension = ltrim($extension, '.');
    // No extension for paths
    if (str_ends_with($path, '/')) {
        return $path;
    }
    // No actual extension in path
    if (!$actualExtension) {
        return $path . (str_ends_with($path, '.') ? '' : '.') . $extension;
    }
    return substr($path, 0, -\strlen($actualExtension)) . $extension;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal