<?php
// input: filename
// ouptut: filename extention
// or
// -1
function fileGetExtention( $filename ) {
$retvalue = "";
$tmp = explode( ".", $filename );
if( $tmp[1] ) {
$retvalue = $tmp[ count( $tmp ) - 1 ];
}
else {
$retvalue = -1;
}
return $retvalue ;
} // function
// Self Validation Code
// This portion only runs if this script is called directly.
// This script is meant to be 'included' into another PHP script
if( basename( $GLOBALS["PHP_SELF"] ) == "lib_file.php" ) {
$test_pass_values = array( "./foo.bar",
"./this_is/a/long_directory/foo.bar",
"./this_is/a/long_directory/foo.bar.test.text",
"file.bak" );
$test_fail_values = array( "",
"...",
"3",
"file",
array( "asd", "wer.txt" ) );
echo "<p>passing tests</p>\n";
while( list( $k, $v ) = each( $test_pass_values ) ) {
echo "$v >>> " . fileGetExtention( $v ) . "<br>\n";
}
echo "<p>failing tests</p>\n";
while( list( $k, $v ) = each( $test_fail_values ) ) {
echo "$v >>> " . fileGetExtention( $v ) . "<br>\n";
}
}
?>
|