3v4l.org

run code in 300+ PHP versions simultaneously
<?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); }

preferences:
21.28 ms | 409 KiB | 5 Q