<?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('');
/**
* 文件编码转换
*
*/
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('');