PHP: How to write file to disk with unicode characters

孤街醉人 提交于 2019-12-11 12:16:29

问题


I need to write out a file to disk with special ISO-8859-15 characters. For my own testing purposes, I used:

—©®±àáâãäåæÒÓÔÕÖ¼½¾§µçðþú–.jpg

...but the em-dash, en-dash, and the 1/2, 1/4, and 3/4 fractions were replaced with garbage when the file was written to disk with this name, while the other characters in the file name were written out correctly. Why some and not others???

Here is a very simple PHP script to write out a file with just copyright symbols and em-dashes in its name. When I run it, the string is written to the file correctly, but the filename's em-dashes are replaced with garbage:

<?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);
?>

What is the most efficient and elegant way to do this using PHP (or Javascript)? I'm targeting the ISO-8859-15 character set ONLY.

Many thanks! Tom


回答1:


I've found my own answer. First, I need WINDOWS-1252 encoding, as it turns out. Second, all I need to do is use inconv(), converting from 'UTF-8' to 'WINDOWS-1252', like so:

<?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);
?>

My only lingering question, provided that I'm testing this on XAMPP on my local Windows machine, is whether WINDOWS-1252 encoding will work on actual servers at the major hosting services (GoDaddy, etc.) If not, is there a different encoding that supports everything included in WINDOWS-1252 but better suited for non-XAMPP localhost servers?

There's a complete listing of encodings supported by iconv here. Several are on the same line as WINDOWS-1252; does that mean they are interchangeable?

Many thanks, Tom



来源:https://stackoverflow.com/questions/28466995/php-how-to-write-file-to-disk-with-unicode-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!