PHP:如何使用Unicode字符将文件写入磁盘 [英] PHP: How to write file to disk with unicode characters

查看:82
本文介绍了PHP:如何使用Unicode字符将文件写入磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

-©®±àáâãäåæÒÓÒÓ¼½½¾§µçðþú ..

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

...但是当使用此名称将文件写入磁盘时,将破折号,破折号和1/2、1/4和3/4分数替换为垃圾文件名中的正确写出.为什么有些而不是其他??

...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???

这是一个非常简单的PHP脚本,用于写出名称中仅带有版权符号和破折号的文件.当我运行它时,字符串正确地写入了文件,但是文件名的破折号被替换为垃圾:

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

使用PHP(或Javascript)执行此操作的最有效,最优雅的方法是什么?我的目标只是ISO-8859-15字符集.

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.

非常感谢! 汤姆

推荐答案

我已经找到了自己的答案.首先,事实证明,我需要WINDOWS-1252编码.其次,我要做的就是使用inconv(),将其从"UTF-8"转换为"WINDOWS-1252",就像这样:

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

我要在本地Windows计算机上的XAMPP上对此进行测试的唯一一个挥之不去的问题是,WINDOWS-1252编码是否可以在主要托管服务(例如GoDaddy等)的实际服务器上运行?可以支持WINDOWS-1252中包含的所有内容,但更适合于非XAMPP本地服务器的另一种编码吗?

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?

这里有iconv支持的完整编码列表

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?

非常感谢, 汤姆

这篇关于PHP:如何使用Unicode字符将文件写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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