简体   繁体   English

字符编码问题和PHP-这是什么编码?

[英]Character encoding issues and PHP - what encoding is this?

I have a file on my computer that I wanted to copy into a MySQL table using PHP. 我的计算机上有一个文件,希望使用PHP复制到MySQL表中。 When I open the file the contents look fine, like normal text - but, when I attempt to read the file using PHP or insert into a MySQL table, I get all sorts of funky characters. 当我打开文件时,内容看起来像普通文本一样好-但是,当我尝试使用PHP读取文件或将其插入到MySQL表中时,会得到各种各样的时髦字符。 I thought perhaps it was a utf-8 issue, so I tried setting the header 我以为这可能是utf-8问题,所以我尝试设置标头

header('Content-type: text/html; charset=utf-8');

And then echoing the contents retrieved by file_get_contents(), but that didn't make any difference - the output was still funky. 然后回显由file_get_contents()检索的内容,但这没有任何区别-输出仍然很时髦。 I then thought perhaps it was a cp1252 issue so I tried using htmlentities() but that also didn't help: 然后我以为这可能是cp1252问题,所以我尝试使用htmlentities(),但这也无济于事:

htmlentities($str, ENT_QUOTES, 'cp1252')

I then uploaded the file to a website ( here ). 然后,我将文件上传到网站( 此处 )。 On the server when I 'cat' the file, it again looks normal, but in Firefox when I open it I get the funky characters. 在服务器上,当我“捕获”文件时,它看起来又正常了,但是在Firefox中,当我打开它时,我得到了一些时髦的字符。 Here's a screenshot of what it looks like to me: screenshot 这是我的屏幕截图屏幕截图

Oddly I copied the same exact file to another website's folder on the same server, and yet when I open this file at the new URL (see here ) the same file appears different in Firefox - still some funky characters, but less of them. 奇怪的是,我将相同的确切文件复制到同一服务器上的另一个网站的文件夹中,但是当我在新的URL中打开该文件时(请参阅此处 ),该文件在Firefox中看起来有所不同-仍然有些时髦的字符,但是字符较少。 A screenshot of the different appearance: screenshot 不同外观的屏幕截图屏幕截图

Does anybody know what's going on here, and how I can clean the characters up? 有人知道这是怎么回事,我怎么清理角色? What character encoding is this file using - and why does the same file look different in Firefox when copied from one website on the server to another? 此文件使用什么字符编码-从服务器上的一个网站复制到另一个网站时,为什么同一文件在Firefox中看起来有所不同?

Your file is in UTF-16; 您的文件位于UTF-16中; try using that as the encoding in Firefox. 尝试将其用作Firefox中的编码。 It looks much more correct, though there are still some stray CJK characters, which may be because some of your characters are in the wrong endianness. 它看起来更正确,尽管仍然有一些CJK杂散字符,这可能是因为您的某些字符的字节序错误。

Please remember to accept an answer on all of your questions where a good answer was provided; 请记住,在所有问题均得到良好答案的情况下接受答案; many people on Stack Overflow may not continue to answer your questions with a lack of reputation incentive. 缺乏声誉激励的情况下,Stack Overflow上的许多人可能无法继续回答您的问题。

I've had this issue before. 我以前遇到过这个问题。 There are hidden characters which CANNOT be displayed in certain IDE's. 隐藏的字符无法在某些IDE中显示。

I was able to resolve this by opening the file in Notepad and and copying the text, then deleting the file. 通过在记事本中打开文件并复制文本,然后删除文件,我能够解决此问题。 I then created the file from scratch and pasted in just the plain text. 然后,我从头开始创建文件,然后仅粘贴纯文本。

You do not want to copy the text with something like wordpad as that will also copy the hidden characters. 您不想使用写字板之类的东西来复制文本,因为这也会复制隐藏的字符。

EDIT: 编辑:

You may also want to try accepting some peoples answers for your previous questions as you will get more replies from people willing to help if they know you will provide reputation. 您可能还想尝试接受某些人对您以前的问题的回答,因为如果愿意帮助您的人知道他们会提供声誉,您会得到他们的更多答复。

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

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