且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP scandir()和htmlentities():charset和/或特殊字符的问题

更新时间:2023-01-18 19:04:38

我***的猜测是文件名本身没有使用UTF-8。或者至少 scandir()不会这样拾取。

My best guess is that the filename itself isn't using UTF-8. Or at least scandir() isn't picking it up like that.

也许

var_dump(mb_detect_encoding($filename));

如果没有,尝试猜测编码(CP1252或ISO-8859-1将是我的第一个猜测)并将其转换为UTF-8,查看输出是否有效:

If not, try to guess the encoding (CP1252 or ISO-8859-1 would be my first guess) and convert it to UTF-8, see if the output is valid:

var_dump(mb_convert_encoding($filename, 'UTF-8' 'Windows-1252'));
var_dump(mb_convert_encoding($filename, 'UTF-8' 'ISO-8859-1'));
var_dump(mb_convert_encoding($filename, 'UTF-8' 'ISO-8859-15'));

或使用 iconv()



Or using iconv():

var_dump(iconv('WINDOWS-1252', 'UTF-8', $filename));
var_dump(iconv('ISO-8859-1',   'UTF-8', $filename));
var_dump(iconv('ISO-8859-15',  'UTF-8', $filename));

然后当你弄清楚实际使用的编码,你的代码应该看起来像这样假设CP1252):

Then when you've figured out which encoding is actually used, your code should look somewhat like this (assuming CP1252):

$filename = htmlentities(mb_convert_encoding($filename, 'UTF-8' 'Windows-1252'), ENT_QUOTES, 'UTF-8');