@ 2015-02-05T23:01:03Z <?php
$str = "Jim & Bob's (amazing!) Photo #2 w/Susan [@Melbourne, Australia].jpg";
echo nl2br($str ."\n");
echo sanitizeFilename($str);
function sanitizeFilename($f) {
//FROM: http://www.house6.com/blog/?p=83
// a combination of various methods
// we don't want to convert html entities, or do any url encoding
// we want to retain the "essence" of the original file name, if possible
// char replace table found at:
// http://www.php.net/manual/en/function.strtr.php#98669
/*
Usage example:
$str = "Jim & Bob's (amazing!) Photo #2 [@Melbourne, Australia].jpg";
echo sanitizeFilename($str);
// Outputs:
// jim-and-bobs-amazing-photo-number-2-at-melbourne-australia.jpg
*/
$replace_chars = array(
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
);
$f = strtr($f, $replace_chars);
// convert & to "and", @ to "at", and # to "number"
$f = preg_replace(array('/[\&]/', '/[\@]/', '/[\#]/' ), array('-and-', '-at-', '-number-'), $f);
$f = preg_replace('/[^(\x20-\x7F)]*/','', $f); // removes any special chars we missed
$f = str_replace(' ', '-', $f); // convert space to hyphen
$f = str_replace('\'', '', $f); // removes apostrophes
$f = preg_replace('/[^\w\-\.]+/', '', $f); // remove non-word chars (leaving hyphens and periods)
$f = preg_replace('/[\-]+/', '-', $f); // converts groups of hyphens into one
$abbrev_words = [
['word' => 'S', 'abbrev' => 'South'],
['word' => 'W', 'abbrev' => 'West'],
['word' => 'N', 'abbrev' => 'North'],
['word' => 'E', 'abbrev' => 'East'],
];
foreach ($abbrev_words as $row) {
$search[] = '/(?<=\s|^)' . preg_quote($row['word'], '/') . '\b/i';
$abbrev[] = $row['abbrev'];
}
$title = "Heaven's Basement in W Virginia";
echo preg_replace($search, $abbrev, $title);
return strtolower($f);
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.30 , 8.2.0 - 8.2.25 , 8.3.0 - 8.3.13 Jim & Bob's (amazing!) Photo #2 w/Susan [@Melbourne, Australia].jpg<br />
Heaven's Basement in West Virginiajim-and-bobs-amazing-photo-number-2-wsusan-at-melbourne-australia.jpg Output for 4.4.2 - 4.4.9 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/HVdo5 on line 46
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 , 5.0.0 - 5.0.5 Parse error: parse error, unexpected '[' in /in/HVdo5 on line 46
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error in /in/HVdo5 on line 46
Process exited with code 255 . preferences:dark mode live preview
71.24 ms | 409 KiB | 5 Q