Home > front end >  PHP - strtr function with array not working
PHP - strtr function with array not working

Time:06-26

I am currently migrating a Wordpress website to a new system I just created. I zipped the project and uploaded it to the server via WinSCP. A lot of the image filenames contain accents(e.g é) but didn't retain the accents after unzipping.

For example Nestlé-Coffee-Mate-Chocolat-Crème-425g.jpg` became

Nestl#U00e9-Coffee-Mate-Chocolat-Cr#U00e8me-425g.jpg

on the server.

I am trying to write a script to read all the image files and rename them so that they get their accents back.

Based off this solution, I am trying something like this:

public function rename() {
        $photos = array_diff(scandir(FCPATH . 'images/test' ), ['.', '..']);

        $utf8_ansi2 = array(
            "#u00c0" =>"À",
            "#u00c1" =>"Á",
            "#u00c2" =>"Â",
            "#u00c3" =>"Ã",
            "#u00c4" =>"Ä",
            "#u00c5" =>"Å",
            "#u00c6" =>"Æ",
            "#u00c7" =>"Ç",
            "#u00c8" =>"È",
            "#u00c9" =>"É",
            "#u00ca" =>"Ê",
            "#u00cb" =>"Ë",
            "#u00cc" =>"Ì",
            "#u00cd" =>"Í",
            "#u00ce" =>"Î",
            "#u00cf" =>"Ï",
            "#u00d1" =>"Ñ",
            "#u00d2" =>"Ò",
            "#u00d3" =>"Ó",
            "#u00d4" =>"Ô",
            "#u00d5" =>"Õ",
            "#u00d6" =>"Ö",
            "#u00d8" =>"Ø",
            "#u00d9" =>"Ù",
            "#u00da" =>"Ú",
            "#u00db" =>"Û",
            "#u00dc" =>"Ü",
            "#u00dd" =>"Ý",
            "#u00df" =>"ß",
            "#u00e0" =>"à",
            "#u00e1" =>"á",
            "#u00e2" =>"â",
            "#u00e3" =>"ã",
            "#u00e4" =>"ä",
            "#u00e5" =>"å",
            "#u00e6" =>"æ",
            "#u00e7" =>"ç",
            "#u00e8" =>"è",
            "#u00e9" =>"é",
            "#u00ea" =>"ê",
            "#u00eb" =>"ë",
            "#u00ec" =>"ì",
            "#u00ed" =>"í",
            "#u00ee" =>"î",
            "#u00ef" =>"ï",
            "#u00f0" =>"ð",
            "#u00f1" =>"ñ",
            "#u00f2" =>"ò",
            "#u00f3" =>"ó",
            "#u00f4" =>"ô",
            "#u00f5" =>"õ",
            "#u00f6" =>"ö",
            "#u00f8" =>"ø",
            "#u00f9" =>"ù",
            "#u00fa" =>"ú",
            "#u00fb" =>"û",
            "#u00fc" =>"ü",
            "#u00fd" =>"ý",
            "#u00ff" =>"ÿ"
        );

        foreach ($photos as $photo) {          
            $newFilename = strtr($photo, $utf8_ansi2);
            var_dump($newFilename); die;

            rename (FCPATH . 'images/test/' . $photo, FCPATH . 'images/test/' . $newFilename);
        }
    }

For testing purposes I created a folder test and put only one file in it, but the var_dump after using strtr still returns

Nestl#U00e9-Coffee-Mate-Chocolat-Cr#U00e8me-425g.jpg

CodePudding user response:

strtr is case sensitive. Maybe this will help you.

Your array contains lowercase u's, your file name uppercase ones.

  •  Tags:  
  • php
  • Related