Following is not the most efficient solution out there. But if you are fan of regex, it is easy to read and maintain.

/**
 * Change extension of a given filepath. If filename does not have extension,
 * the new extension is simply appended.
 *
 * @param string $newExt May or may not start with .
 */
function changeExtension(string $filepath, string $newExt): string 
{
    $ext = str_starts_with($newExt, '.') ? $newExt : '.' . $newExt;
    return preg_replace('/\\.[^.]+$/', '', $filepath) . $ext;
}