@ 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 git.master , git.master_jit , rfc.property-hooks 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 This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
53.04 ms | 405 KiB | 5 Q