PHP:如何用unicode字符将文件写入磁盘


PHP: How to write file to disk with unicode characters

我需要用特殊的ISO-8859-15字符将文件写入磁盘。出于我自己的测试目的,我使用了:

--©

但是,当使用该名称将文件写入磁盘时,em、en和1/2、1/4和3/4部分被垃圾替换,而文件名中的其他字符则被正确地写出。为什么有些而不是其他???

这里有一个非常简单的PHP脚本,用于编写一个名称中只有版权符号和em短划线的文件。当我运行它时,字符串被正确地写入文件,但文件名的em破折号被垃圾取代:

<?php
    // First, create a text file with the em-dash and the copyright symbol, then put the file prefix into the file:
    $filename1 = "000—©—©.txt";
    $content1 = "000—©—©";
    file_put_contents($filename1, $content1);
?>

使用PHP(或Javascript)最有效、最优雅的方法是什么?我只针对ISO-8859-15字符集。

非常感谢!Tom

我找到了自己的答案。首先,事实证明,我需要WINDOWS-1252编码。其次,我所需要做的就是使用inconv(),从"UTF-8"转换为"WINDOWS-1252",如下所示:

<?php
    // First, create a text file with the em-dash and the copyright symbol, then put the file prefix into the file:
    $filename1 = "000—©—©.txt";
    $content1 = "000—©—©";
    // Judicious use of iconv() does the trick:
    $filename1 = iconv('UTF-8', 'WINDOWS-1252', $filename1);
    file_put_contents($filename1, $content1);
?>

如果我在本地Windows机器上的XAMPP上测试这一点,我唯一挥之不去的问题是,Windows-1252编码是否能在主要托管服务(GoDaddy等)的实际服务器上工作。如果不能,是否有一种不同的编码支持Windows-1252中包含的所有内容,但更适合非XAMPP本地主机服务器?

这里有iconv支持的编码的完整列表。几个与WINDOWS-1252在同一条线上;这是否意味着它们可以互换?

非常感谢,Tom