使用PHP检查csv文件是否在UTF-8中 [英] Check if csv file is in UTF-8 with PHP

查看:300
本文介绍了使用PHP检查csv文件是否在UTF-8中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在没有BOM编码的情况下检查CSV文件的UTF-8?我要检查整个文件,而不是单个字符串.

Is there a way which checks a CSV-file for UTF-8 without BOM encoding? I want to check the whole file and not a single string.

我会尝试用特殊字符设置第一行,而不是读取字符串并检查它是否与脚本中硬编码的相同字符串相匹配.但是我不知道这是个好主意.

I would try to set the first line with a special character and than reading the string and checking if it matches the same string hard-coded in my script. But I don't know if this is a good idea.

仅Google 告诉了我.但是上一篇文章中的链接不可用.

Google only showed me this. But the link in the last post isn't available.

推荐答案

if (mb_check_encoding(file_get_contents($file), 'UTF-8')) {
    // yup, all UTF-8
}

如果文件很大并且您不想一次将其全部存储在内存中,则也可以使用fgets逐行进行处理.不知道问题的第二部分是什么意思.

You can also go through it line by line with fgets, if the file is large and you don't want to store it all in memory at once. Not sure what you mean by the second part of your question.

这篇关于使用PHP检查csv文件是否在UTF-8中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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