Location: PHPKode > scripts > STRIP-EXTENSION.PHP > STRIP-EXTENSION.PHP
<?
function strip_extensions_1($file) {
    $args = func_get_args();
    
    for ($i = 1; $i < count($args); $i++) {
        $ext = $args[$i];
        
        $ext_pos = strrpos($file, '.' . $ext);
        
        //The extension must exist and be at the end of the file name.
        if ($ext_pos != false && $ext_pos + strlen($ext) == strlen($file) - 1) {
            return(substr($file, 0, $ext_pos));
        }
    }
    
    return($file);
}

function strip_extensions_2($file) {
    $pathinfo = pathinfo($file);
    
    $args = func_get_args();
    
    //If no extensions are provided, removes any extension, regardless of what it is.
	
    if (count($args) < 2) {
        return($pathinfo['dirname'] . $pathinfo['filename']);
    } else {
		
        $args = array_splice($args, 1);
        
        if (array_search($pathinfo['extension'], $args) === false && 
		    isset($pathinfo['extension'])) {
            return($pathinfo['dirname'] . $pathinfo['filename'] . '.' . $pathinfo['extension']);
        } else {
            return($pathinfo['dirname'] . $pathinfo['filename']);
        }
    }
    
}

$path = '../../*';
$files = glob($path);

foreach($files as $file) {
    echo(strip_extensions_1($file, 'php', 'js') . '<br />');
}

echo('<br /><hr /><br />');

foreach($files as $file) {
    echo(strip_extensions_2($file, 'php', 'js') . '<br />');
}

echo('<br /><hr /><br />');

foreach($files as $file) {
    echo(strip_extensions_2($file) . '<br />');
}

echo('<br /><hr /><br />');

foreach($files as $file) {
    print_r(pathinfo($file));
    echo('<br />');
}
?>
Return current item: STRIP-EXTENSION.PHP