bash - PHP filename encoding conversion issue -
i'm trying batch rename files in folder php. it's working, though i'm having problems accented characters.
an illustration of filename accented characters Åre_grÖn.jpg.
i rename file are_gron.jpg.
if read files in this:
<?php $path = __dir__; $dir_handle = opendir($path); while ($file = readdir($dir_handle)) { echo $file . "\n"; } closedir($dir_handle); ...and page displays åre_grön.jpg.
if add together header('content-type: text/html; charset=utf-8'); origin of script, displays right file name, rename() function seems have no effect either way.
here's i've tried:
while ($file = readdir($dir_handle)) { rename($file, str_replace('ö', 'o', $file)); # no effect rename($file, str_replace('Ö', 'o', $file)); # no effect } where going wrong?
do if believe i'm using wrong tool job. if knows how accomplish bash script, show me. have no bash chops.
i figured out how it.
i first ran urlencode() on filename. converts string:
mÖrkgrÅ.jpg to url friendly:
mo%cc%88rkgra%cc%8a.jpg i ran str_replace() on url-encoded string, providing needles , haystacks in arrays. needed few swedish characters, solution looked this:
<?php header('content-type: text/html; charset=utf-8'); $path = __dir__; $dir_handle = opendir($path); while ($file = readdir($dir_handle)) { $search = array('a%cc%8a', 'a%cc%88', 'o%cc%88'); $replace = array('a', 'a', 'o'); rename($file, str_replace($search, $replace, urlencode($file))); } closedir($dir_handle); job done :)
i've come realise more versatile anticipated. running script, url_encode() gave me different output, it's easy alter accordingly.
$search = array('%26aring%3b', '%26auml%3b', '%26ouml%3b', '+'); $replace = array('a', 'a', 'o', '_'); php bash
No comments:
Post a Comment