简体   繁体   English

使用 UTF-8 特殊字符处理 PHP 文件

[英]PHP File Handling with UTF-8 Special Characters

It seems that File Handling in PHP doesn't work with certain characters (eg €) encoded UTF-8 if the path is hardcoded and the php-file saved as UTF-8.如果路径是硬编码的并且 php 文件保存为 UTF-8,那么 PHP 中的文件处理似乎不适用于某些字符(例如 €)编码的 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.因为我找不到。

For example a folder named äöü&()éèàâêûô@$+ç%&= !£_;{[]}~´¢¬§°#@¦…€` in windows won't work with is_dir().例如,Windows 中名为äöü&()éèàâêûô@$+ç%&= !£_;{[]}~´¢¬§°#@¦...€` 的文件夹不能与 is_dir() 一起使用。

For Windows the solution is to convert filename / folder name to Windows-1252 encoding:对于 Windows,解决方案是将文件名/文件夹名称转换为 Windows-1252 编码:

$dir = 'فارسی';
$dir = iconv(mb_detect_encoding($dir, "auto"), 'Windows-1252', $string);
mkdir($dir);

(Note: this answer was added to the answer by the original user , and has been moved here to better fit the site format.) (注意:这个答案是由原始用户添加到答案中的,为了更好地适应网站格式,已移至此处。)

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).我还没有完全测试它,我不确定它是否万无一失,我不知道这是否是最佳实践,但将字符串编码回 ANSI 似乎可以解决问题(至少对于我发布的字符串)。

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

I guess this should work with the default setting of most western windows computers.我想这应该适用于大多数西方 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-16,因为这应该是 Windows 内部使用的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM