note 80412 modified in function.preg-replace by danbrown

From: Date: Fri, 28 Nov 2008 19:49:04 +0000
Subject: note 80412 modified in function.preg-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147143@lists.php.net to get a copy of this message
Here is my attempt at cleaning up a file name... it's similar to what someone else has done however a little cleaner with the addition of the | in the reserved characters... also I clean any characters from x00 to x40 (all non display characters and space) as well as everything greater than 7f and greater (removes the Del character and other non English characters), replacing them with an '_'. <?php function clean_filename($filename){//function to clean a filename string so it is a valid filename $reserved = preg_quote('\/:*?"<>|', '/');//characters that are illegal on any of the 3 major OS's //replaces all characters up through space and all past ~ along with the above reserved characters return preg_replace("/([\\x00-\\x20\\x7f-\\xff{$reserved}])/e", "_", $filename); } ?> [EDIT BY danbrown AT php DOT net: Inserted typofix/bugfix provided by "Bryan Roach" on 15 January, 2008.] --was-- Here is my attempt at cleaning up a file name... it's similar to what someone else has done however a little cleaner with the addition of the | in the reserved characters... also I clean any characters from x00 to x40 (all non display characters and space) as well as everything greater than 7f and greater (removes the Del character and other non English characters), replacing them with an '_'. function clean_filename($filename){//function to clean a filename string so it is a valid filename $reserved = preg_quote('\/:*?"<>|', '/');//characters that are illegal on any of the 3 major OS's //replaces all characters up through space and all past ~ along with the above reserved characters return preg_replace("/([\\x00-\\x40\\x7f-\\xff{$reserved}])/e", "_", $filename); } http://php.net/manual/en/function.preg-replace.php

« previous php.notes (#147143) next »