Safe name of a file by 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);
    }

Comments

Popular posts from this blog

Sort Products by popularity count in Magento