3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function sanitizeFilename($name) { $res = preg_replace('#\w:\s#', ' - ', $name); $res = preg_replace('#\s:\s#', '', $res); $res = strtr($res, [ '<' => '-', '>' => '-', ':' => '-', '"' => '', '/' => '-', '\\' => '-', '|' => '-', '?' => '', '*' => '-' ]); $res = preg_replace('#\s{2,}#', ' ', $res); return $res; } var_dump(sanitizeFilename('The Hobbit: an unexpected')); var_dump(sanitizeFilename('The Hobbit:forever')); var_dump(sanitizeFilename('The Hobbit 50:50')); var_dump(sanitizeFilename('The Hobbit 50 : 50'));
based on 1oeUF
Output for 5.4.0 - 7.2.0
string(25) "The Hobbi - an unexpected" string(18) "The Hobbit-forever" string(16) "The Hobbit 50-50" string(15) "The Hobbit 5050"
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/oCCeG on line 7
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/oCCeG on line 7
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/oCCeG on line 7
Process exited with code 255.