开发者

How to get str_replace to recognize umlauts in file names retrieved with glob (on a Mac)?

I'm reading file names 开发者_JAVA技巧out of a directory with glob with the code below.

I want to iterate through them and replace umlaut characters with other characters.

However, str_replace does not even find the umlaut characters, so I assume they are in some other kind of character encoding.

This has always worked on Windows but I am now on a Mac so I tried to convert from "macintosh" to "UTF-8" with iconv but that didn't work, see below.

If I define a string with umlauts in code, then str_replace finds the umlauts fine but not in the strings retrieved with glob().

How can I get str_replace to recognize the umlaut characters in file name strings so I can replace them?

$pathAndFileNames = glob($directory . '/*.php');
if (count($pathAndFileNames) > 0) {
    foreach ($pathAndFileNames as $oldName) {

        $newName = str_replace('ü', 'ue', $oldName);
        echo $newName; //outputs "rücktest.php"

        $oldName2 = "rücktest.php";
        $newName2 = str_replace('ü', 'ue', $oldName2);
        echo $newName2; //outputs "ruecktest.php"

    }
}


I tested this code on my machine (Ubuntu mind you) and had no such issue. I have one solution for you to try. Make sure your script is saved with UTF-8 encoding.

I can replicate your issue when saving the file with Western-8859-15 encoding, but when the file is saved with UTF-8 encoding the script behaves as you would expect.

Check your editor preferences and save as functionality.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜