具有UTF-8特殊字符的PHP文件处理 [英] PHP File Handling with UTF-8 Special Characters

查看:115
本文介绍了具有UTF-8特殊字符的PHP文件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎PHP中的文件处理不适用于某些字符(例如€)编码的UTF-8.如果路径是硬编码的,并且php文件另存为UTF-8.

It seems that File Handling in PHP doesn't work with certain characters (e.g. €) encoded UTF-8 if the path is hardcoded and the php-file saved as UTF-8.

是否可以使其与所有(或大多数西方字符)一起使用?有没有可能使之成为可能的图书馆?因为我找不到任何东西.

Is it possible to make it work with all (or most western characters)? Is there a library which makes it possible maybe? Because I couldn't find any.

例如,在Windows中名为äöü&()éèàâêûô@$+ç%&=!£_; {[]}〜´¢§§#@ ¦ ...€`的文件夹将无法与is_dir()一起使用.

For example a folder named äöü&()éèàâêûô@$+ç%&=!£_;{[]}~´¢¬§°#@¦…€` in windows won't work with is_dir().

好的,我找到了解决方案.我还没有完全测试它,我不确定它是否万无一失,我也不知道这是否是最佳实践,但是将字符串编码回ANSI似乎可以解决问题(至少对于我发布的字符串而言) ).

Ok, I have found a solution. I haven't tested it fully yet, I'm not sure if it's foolproof yet and I don't know if this is the best practise but encoding the string back to ANSI seems to do the trick (at least for the string I posted).

$string = iconv(mb_detect_encoding($string, "auto"), 'Windows-1252', $string);

我想这应该适用于大多数西方Windows计算机的默认设置.

I guess this should work with the default setting of most western windows computers.

推荐答案

我也拒绝使用这样的文件夹! :-)
开个玩笑,您可能会尝试将文件另存为UTF-16,因为这应该是Windows内部使用的编码.

I'd refuse to work with such a folder too! :-)
Jokes apart, you might try to save the file as UTF-16, as this is supposed to be the encoding Windows uses internally.

这篇关于具有UTF-8特殊字符的PHP文件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆