Convert PHP script for URL safe filenames into JavaScript?
<p>I'm developing a one-time conversion tool to migrate hundreds of thousands of user files from one hosting environment to another. As part of the migration, I'm cleaning up filenames to make them URL-safe. My PHP script does this (can't claim it's the most elegant, correct, or optimized code, but it does what I need). File extensions are handled separately, so they're not an issue here.</p>

<pre><code>&lt;?php
$fileName = $_POST['name'];
$swap_chars = array('%20'=&gt;'-', '&amp;'=&gt;'and', '@'=&gt;'at', '='=&gt;'eq', '#'=&gt;'num', '%'=&gt;'pct', '+'=&gt;'-', ' '=&gt;'-', ','=&gt;'-', '/'=&gt;'-', ':'=&gt;'-', ';'=&gt;'-', '\\'=&gt;'-', '|'=&gt;'-', '~'=&gt;'-', 'Š'=&gt;'S', 'š'=&gt;'s', 'Ð'=&gt;'Dj', 'Ž'=&gt;'Z', 'ž'=&gt;'z', 'À'=&gt;'A', 'Á'=&gt;'A', 'Â'=&gt;'A', 'Ã'=&gt;'A', 'Ä'=&gt;'A', 'Å'=&gt;'A', 'Æ'=&gt;'A', 'Ç'=&gt;'C', 'È'=&gt;'E', 'É'=&gt;'E', 'Ê'=&gt;'E', 'Ë'=&gt;'E', 'Ì'=&gt;'I', 'Í'=&gt;'I', 'Î'=&gt;'I', 'Ï'=&gt;'I', 'Ñ'=&gt;'N', 'Ò'=&gt;'O', 'Ó'=&gt;'O', 'Ô'=&gt;'O', 'Õ'=&gt;'O', 'Ö'=&gt;'O', 'Ø'=&gt;'O', 'Ù'=&gt;'U', 'Ú'=&gt;'U', 'Û'=&gt;'U', 'Ü'=&gt;'U', 'Ý'=&gt;'Y', 'Þ'=&gt;'B', 'ß'=&gt;'ss', 'à'=&gt;'a', 'á'=&gt;'a', 'â'=&gt;'a', 'ã'=&gt;'a', 'ä'=&gt;'a', 'å'=&gt;'a', 'æ'=&gt;'a', 'ç'=&gt;'c', 'è'=&gt;'e', 'é'=&gt;'e', 'ê'=&gt;'e', 'ë'=&gt;'e', 'ì'=&gt;'i', 'í'=&gt;'i', 'î'=&gt;'i', 'ï'=&gt;'i', 'ð'=&gt;'o', 'ñ'=&gt;'n', 'ò'=&gt;'o', 'ó'=&gt;'o', 'ô'=&gt;'o', 'õ'=&gt;'o', 'ö'=&gt;'o', 'ø'=&gt;'o', 'ù'=&gt;'u', 'ú'=&gt;'u', 'û'=&gt;'u', 'ü'=&gt;'u', '†'=&gt;'t', '°'=&gt;'deg', '¢'=&gt;'c', '£'=&gt;'L', '§'=&gt;'S', '•'=&gt;'o', '¶'=&gt;'P', '®'=&gt;'R', '©'=&gt;'C', '™'=&gt;'TM', 'ý'=&gt;'y', 'ý'=&gt;'y', 'þ'=&gt;'b', 'ÿ'=&gt;'y', 'ƒ'=&gt;'f');
$fileName = str_replace('\\\'', '', $fileName);             // strip escaped apostrophes
$fileName = str_replace('\\"', '', $fileName);              // strip escaped quotes
$fileName = strtr($fileName, $swap_chars);                  // swap special characters
$fileName = preg_replace("/[^0-9a-zA-Z._-]/","",$fileName); // strip remaining bad characters
$fileName = preg_replace("/--+/","-",$fileName);            // trim repeating dashes
$fileName = preg_replace("/\.\.+/",".",$fileName);          // trim repeating periods
$fileName = preg_replace("/__+/","_",$fileName);            // trim repeating underscores
$fileName = trim($fileName, '.-_');                         // remove leading or trailing punctuation
$fileName = substr($fileName, 0, 62);                       // truncate long filenames
echo $fileName;
?&gt;
</code></pre>

<p>Large chunks of the tool are also in JavaScript and I'd like to do the cleanup there instead, if possible. I presume I can probably use <code>text.replace(old,new)</code> to replicate the <code>str_replace()</code> and <code>preg_replace()</code> PHP functions.</p>

<p>However, PHP's <code>strtr()</code> and <code>trim()</code> functions do some pretty cool stuff (strtr can use associative arrays for match/replace; trim strips multiple specified characters) and I haven't found any JS equivalents.</p>

<p>So, is there a simpler way of doing this that I've overlooked, or should I be digging in and writing my own functions? Alternatively, is PHP.js a viable option?</p>

<p><strong>UPDATE w/ Solution:</strong></p>

<p>Using the very quick and helpful answer from @Matt Winckler, I've managed to rewrite my cleanup code in JavaScript. The following works (at least for my use case) in conjunction with the code provided in the accepted answer below, along with a JavaScript object "swapchars" based on the PHP array above.</p>

<pre><code>function cleanname(name) {
    name = name.replace('\'', '');              // strip escaped apostrophes
    name = name.replace('\"', '');              // strip escaped quotes
    name = name.replace('%20', '-');            // replace encoded spaces
    name = strtr(name, swapchars);              // swap special characters
    name = name.replace(/[^0-9a-z_\.\-]/gi,''); // strip remaining bad characters
    name = name.replace(/-+/g,'-');             // trim repeating dashes
    name = name.replace(/\.+/g,'.');            // trim repeating periods
    name = name.replace(/_+/g,'_');             // trim repeating underscores
    name = trim(name, '.-_');                   // remove leading or trailing punctuation
    name = name.substr(0, 62);                  // truncate long names
    return name;
}
</code></pre>
