file_get_contents 读取文件内容
$content = file_get_contents($fileName);
mb_detect_encoding 校验编码
$charset = mb_detect_encoding($content, ['CP936', 'UTF-8']);
// 当在php中使用mb_detect_encoding函数进行编码识别时,很多人都碰到过识别编码有误的问题,
//例如对与GB2312和UTF-8,或者UTF-8和GBK(这里主要是对于cp936的判断),
//网上说是由于字符短时mb_detect_encoding会出现误判。
iconv 或 mb_convert_encoding 来转换编码
$content = mb_convert_encoding($content, 'UTF-8', $charset);
iconv('GBK', 'UTF-8//IGNORE', '测试字符串'); // 将字符串由 GBK 编码转换为 UTF-8 编码
array_filte(array $input, $callback = null, $flag = 0) 回调方法处理字符串
$content = str_replace("\r", '', $content); // 替换字符串
$csvList = explode("\n", $content); // 分割字符串
$csvList = array_filter($csvList); // 回调方法处理字符串 默认就是判断数组值为非空非假留下
instanceof 类型运算符。 用来检测一个给定的对象是否属于(继承于)某个类(class)、某个类的子类、某个接口(interface)。如果是则返回true。
if ($goodsReduction instanceof GoodsReduction) {
return '$goodsReduction 是 GoodsReduction 实例化后的东西';
}else{
return '$goodsReduction 不是类对象的东西';
}
···
本文介绍如何使用PHP的file_get_contents函数读取文件内容,并通过mb_detect_encoding和mb_convert_encoding函数处理文件编码问题,确保正确转换为UTF-8编码。

被折叠的 条评论
为什么被折叠?



