ホームページ  >  記事  >  バックエンド開発  >  PHP でファイルエンコーディングのバッチ変換を実装_PHP チュートリアル

PHP でファイルエンコーディングのバッチ変換を実装_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:36:18802ブラウズ

一部の問題は再現できません。たとえば、gbk を utf8 に変換してから utf8 に変換すると、コードが文字化けします。最初は変換前にエンコードを検出しようとしましたが、失敗したようです。具体的にファイルを試してみて、それが gbk か utf-8 かを確認したところ、どちらも true を返しました。これはわかりません。

コードをコピーします コードは次のとおりです:

/**
* ファイルエンコーディングを変換します
* 依存する拡張ファイルシステムとmbstring
* @example
*

* include_once 'ConvertEncode.php';
* $convert = new ConvertEncode();
* try{
* $convert - >setPath('my', true, true);//ディレクトリ
* //$convert->setPath('my.php');//単一ファイル
* $convert->setEncode('GBK ' , 'UTF-8');
* $convert->convert();
* }catch(ConvertException $e) {
* echo $e->getMessage();
* }
* */
class ConvertEncode {

/**
* 変換されるエンコーディング
* @var string
*/
private $_to_encoding;

/**
* 変換前のエンコーディング
* @var string
*/
private $_from_encoding;

/**
* 変換されるディレクトリまたは単一ファイル
* @var string
*/
private $_path;

/**
* ディレクトリであっても、ディレクトリが指定された場合にのみ設定されます
* @var boolean
*/
private $_directory;

/**
* 再帰的に走査するかどうか、ディレクトリに対してのみ有効です
* @var boolean
*/
private $_recursion;

/**
* 変換するすべてのファイルを保存します。ディレクトリ内のファイルを変換する場合にのみ使用されます
* @var array
*/
private $_files = array();

/**
* コンストラクター
*/
public function __construct() {
if( ! function_exists('mb_convert_encoding') ) {
throw new ConvertException('mbstring extension必須です');
}
}

/**
* 変換する必要があるディレクトリまたは単一ファイルを設定します
* @param string $path ディレクトリまたはファイル
* @param boolean ディレクトリかどうか
* @param boolean 再帰ディレクトリかどうか
* @return boolean
*/
public function setPath($path, $is_dir = false, $rec = false) {
$this->_path = $path;
$this->gt;_directory = $is_dir;
$this->_recursion = $rec;
return true;
}

/**
* 変換前のエンコードと変換後のエンコードを設定します
* @param string $encode 変換前のエンコード
* @param string $encode 変換後のエンコード
* @return boolean
*/
public function setEncode($encode_from, $encode_to) {
$this->_from_encoding = $encode_from;
$this->gt;_to_encoding = $encode_to;
return true;
}

/**
* エンコーディングを変換、ディレクトリ設定かどうかに応じて個別に変換します
* @return boolean
*/
public function Convert() {
if($this- >_directory ) {
return $this->_convertDirectory();
}
return $this->_convertFile();
}

/**
* ファイルを変換します
* @throws ConvertException
* @return boolean
*/
プライベート関数 _convertFile() {
if ( ! file_exists($this->_path) ) {
$message = $this->_path . ' は存在しません。';
throw new ConvertException($message);
}
if( ! is_file($this->_path) ) {
$message = $this->_path . ' はファイルではありません。';
throw new ConvertException($message);
}
if( ! $this->_isWR() ) {
$message = $this->_path . ' は読み取りと書き込みができる必要があります。';
throw new ConvertException($message);
}
$file_real_path = realpath($this->_path);
$file_content_from = file_get_contents( $file_real_path );
if( mb_check_encoding( $file_content_from, $this->_from_encoding) ) {
$file_content_to = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
file_put_contents( $file_real_path, $file_content_to );
}
戻るtrue;

}

/**
* ディレクトリを変換します
* @throws ConvertException
* @return boolean
*/
private function _convertDirectory() {
if( ! file_exists($this->_path) ) {
$message = $this->_path . ' は存在しません。';
throw new ConvertException($message);
}
if( ! is_dir($this->_path) ) {
$message = $this->_path . ' はディレクトリではありません。';
throw new ConvertException($message);
}
if( ! $this->_isWR() ) {
$message = $this->_path . ' は読み取りと書き込みができる必要があります。';
throw new ConvertException($message);
}
$this->_scanDirFiles();
if( empty($this->_files) ) {
$message = $ this->_path 。 ' は空のディレクトリです。';
throw new ConvertException($message);
}
foreach( $this->_files as $value ) {
$file_content_from = file_get_contents( $value );
if( mb_check_encoding($file_content_from) , $this->_from_encoding) ) {
$file_content_to = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
file_put_contents( $value, $file_content_to );
}
}
戻るtrue;
}

/**
* ファイルまたはディレクトリが読み取りおよび書き込み可能かどうかを判断します
* @return boolean は、読み取りおよび書き込みが可能な場合は true を返し、それ以外の場合は false を返します
*/
プライベート関数 _isWR() {
if( is_readable($this->_path) && is_writable($this->_path) ) {
return true;
}
return false;
}

/**
* ディレクトリを走査し、すべてのファイルと絶対パスを検索します
* @return boolean
*/
private function _scanDirFiles($dir = '') {
$base_path = empty( $dir ) ? realpath($this->_path) 。 DIRECTORY_SEPARATOR : realpath($dir) 。 DIRECTORY_SEPARATOR;
$files_tmp = empty( $dir ) ? scandir($this->_path) : scandir($dir);
foreach( $files_tmp as $value ) {
if( $value == '.' || $value == '..' || ( strpos ($value, '.') === 0 ) ) {
続行;
}
$value = $base_path 。 $value;
if( is_dir($value) ) {
if( $this->_recursion ) {
$this->_scanDirFiles($value);
}
}
elseif( is_file($value) ) {
$this->_files[] = $value;
}
}
return true;
}
}

/**
* 変換例外
*
*/
class ConvertException extends Exception {

}

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/739782.html技術記事問題がいくつかあり、gbk が utf8 に移行した後、utf8 に移行すると、このような乱コードが発生するため、移行を繰り返すことができません。本来は移行前にコードを削除し、現象が失われているようです。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。