I get images files which have Czech characters in the filename (eg, ěščřžýáíé) and I want to rename them without the accents so that they are more compatible for the web. I thought I could use a simple str_replace function but it doesn't seem to work the same with the file array as it does with a string literal.
I read the files with readdir, after checking for extension.
function readFiles($dir, $ext = false) { if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if($ext){ if(end(explode('.', $file)) == $ext) { $f[] = $file; } } else { $f[] = $file; } } closedir($dh); return $f; } else { return false; } } else { return false; } } $files = readFiles(".", "jpg"); $search = array('š','á','ž','í','ě','é','ř','ň','ý','č',' '); $replace = array('s','a','z','i','e','e','r','n','y','c','-'); $string = "čšěáýísdjksnalci sášěééalskcnkkjy+ěéší"; $safe_string = str_replace($search, $replace, $string); echo '<pre>'; foreach($files as $fl) { $safe_files[] = str_replace($search, $replace, $fl); } var_dump($files); var_dump($safe_files); var_dump($string); var_dump($safe_string); echo '</pre>'; Output
array(6) { [0]=> string(21) "Hl�vka s listem01.jpg" [1]=> string(23) "Hl�vky v atelieru02.jpg" [2]=> string(17) "Jarn� v�hon03.jpg" [3]=> string(17) "Mlad� chmel04.jpg" [4]=> string(23) "Stavba chmelnice 05.jpg" [5]=> string(21) "Zimni chmelnice06.jpg" } array(6) { [0]=> string(21) "Hl�vka-s-listem01.jpg" [1]=> string(23) "Hl�vky-v-atelieru02.jpg" [2]=> string(17) "Jarn�-v�hon03.jpg" [3]=> string(17) "Mlad�-chmel04.jpg" [4]=> string(23) "Stavba-chmelnice-05.jpg" [5]=> string(21) "Zimni-chmelnice06.jpg" } string(53) "čšěáýísdjksnalci sášěééalskcnkkjy+ěéší" string(38) "cseayisdjksnalci-saseeealskcnkkjy+eesi" Right now I'm running on WAMP but answers that work across platforms are even better :)
iso-8859-1.. that's on windows, I'll try soon on my *nix based hosting and see what happens there..