文件编码转换

<?php
/**
 * 文件编码转换
 * 
 */
class Transcoding
{
/**

* 文件路径
* @var $_filePath
*/
protected $_filePath = null;

/**

* 源编码
* @var $_inCharset
*/
protected $_inCharset = 'UTF-8';

/**

* 转换后的编码
* @var $_outCharset
*/
protected  $_outCharset = 'GBK';

/**

* 构造函数
* @param $filePath
*/
public function __construct($filePath = null)
{
if ($filePath !== null) {
$this->setFilePath($filePath);
}
}

/**

* 设置文件路径
* @param $filePath
*/
public function setFilePath($filePath)
{
if (!empty($filePath)) {
$this->_filePath = $filePath;
return $this;
}
return false;
}

/**

* 取得文件路径
*/
public function getFilePath()
{
return $this->_filePath;
}

/**

* 文件编码转换
* @param $file
* @param $inCharset
* @param $outCharset
*/
public function fileTransCoding($file, $inCharset = null, $outCharset = null)
{
if ($inCharset == null) $inCharset = $this->_inCharset;
if ($outCharset == null ) $outCharset = $this->_outCharset;
$content = @file_get_contents($file);
if ($content) {
if (strtoupper($inCharset) == 'UTF-8') {
if (!self::isUtf8($content)) return false;
} elseif (strtoupper($outCharset) == 'UTF-8') {
if (self::isUtf8($content)) return false;
}
$content = iconv($inCharset, $outCharset . '//IGNORE', $content);
file_put_contents($file, $content);
return true;
}
return false;
}

/**

* 文件夹下文件编码转换
* @param $path
* @param $inCharset
* @param $outCharset
*/
public function dirTransCoding($path, $inCharset = null, $outCharset = null)
{
if ($inCharset == null) $inCharset = $this->_inCharset;
if ($outCharset == null ) $outCharset = $this->_outCharset;
$fileArray = self::lsPath($path, 'file');
foreach ($fileArray as $file) {
self::fileTransCoding($file, $inCharset, $outCharset);
}
}

/**

* 自动编码转换
* @param $filePath
* @param $inCharset
* @param $outCharset
*/
public function autoTransCoding($filePath = null, $inCharset = null, $outCharset = null)
{
if ($filePath == null) $filePath = $this->_filePath;
if ($inCharset == null) $inCharset = $this->_inCharset;
if ($outCharset == null ) $outCharset = $this->_outCharset;
if ($filePath) {
$type = strtolower(@fileType($filePath));
if (method_exists($this, $type . 'TransCoding')) {
$functionName = $type . 'TransCoding';
self::$functionName($filePath, $inCharset, $outCharset);
}
}
return false;
}

/**

* 得到目录下的文件或文件夹列表
* @param $path
* @param $type
*/
static function lsPath($path, $type = 'file')
    {
    $dir = array();
    $files = array();
    $path = rtrim($path, '/');
    $type = strtoupper($type);
    if ($handle = opendir($path)) {
       while ($file = readdir($handle)) {
        if ($file == '.' || $file =='..') continue;
        if (is_dir($path . '/' . $file)) {
           $dir[] = $path . '/' . $file;
        } else {
$files[] = $path . '/' . $file;        
        }
       }
    }
    if ($type == 'DIR') return $dir;
    if ($type == 'FILE') return $files;
    }
    
    /**
     * 
     * 判断字符(串)是否为UTF-8编码
     * @param $str
     */
    static function isUtf8($str)
{
$reg = '/^([\x01-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}'
. '|[\xf0-\xf7][\x80-\xbf]{3}|[\xf8-\xfb][\x80-\xbf]{4}|[\xfc-\xfd][\x80-\xbf]{5})+$/';
if (preg_match($reg, $str)) {
            return true;
} else {
return false;
}
}
}


/*测试示例*/
$transCoding = new Transcoding();
$transCoding->autoTransCoding('');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值