doulu7258 2011-02-21 16:37
浏览 39
已采纳

如何让str_replace识别用glob检索的文件名中的变音符号(在Mac上)?

I'm reading file names 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"

    }
}
  • 写回答

1条回答 默认 最新

  • dph6308 2011-02-21 17:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭