<?php
function filename_safe($filename) {
$temp = $filename;
// Lower case
$temp = strtolower($temp);
// Replace spaces with a '_'
$temp = str_replace(" ", "_", $temp);
// Loop through string
$result = '';
for ($i=0; $i<strlen($temp); $i++) {
if (preg_match('([0-9]|[a-z]|_)', $temp[$i])) {
$result = $result . $temp[$i];
}
}
// Return filename
return $result;
}
function safe_filename ($filename) {
$search = array(
// Definition of German Umlauts START
‘/ß/’,
‘/ä/’,'/Ä/’,
‘/ö/’,'/Ö/’,
‘/ü/’,'/Ü/’,
// Definition of German Umlauts ENDE
‘([^[:alnum:]._])’ // Disallow: Not alphanumeric, dot or underscore
);
$replace = array(
’ss’,
‘ae’,'Ae’,
‘oe’,'Oe’,
‘ue’,'Ue’,
‘_’
);
return preg_replace($search,$replace,$filename);
}
?>
Tag Archive for filename
Filename safe
Replace invalid to valid file name
static string removeBadChar(string filename)
{
// Replace invalid characters with "_" char.
return Regex.Replace(filename, @"[^w.-]", "_");
}
Regular expressions filename replace
// This will strip out any punctuation and spaces from filenames, replacing such characters with underscores
$filename = preg_replace("/[^a-zA-Z0-9s.]/", "_", $filename);
Get current filename
<?php
$currentFile = $_SERVER["PHP_SELF"];
$parts = Explode('/', $currentFile);
echo $parts[count($parts) - 1];
?>
or
<?php
basename($_SERVER[’PHP_SELF’]);
?>
Get a filename extension
//gets a filenames extension
function filename_extension($filename) {//{{{
$pos = strrpos($filename, '.');
if($pos===false) {
return false;
} else {
return substr($filename, $pos+1);
}
}//}}}
Function to Find File Name of Page Displayed
Function getFileName()
Dim lsPath, arPath
'Obtain the virtual file path
lsPath = Request.ServerVariables("SCRIPT_NAME")
'Split the path along the /s. This creates a one-dimensional array
arPath = Split(lsPath, "/")
'The last item in the array contains the file name
getFileName = arPath(UBound(arPath,1))
End Function