PHP轉(zhuǎn)換文件夾下所有文件編碼的實現(xiàn)代碼
更新時間:2013年06月06日 10:58:23 作者:
本篇文章是對PHP轉(zhuǎn)換文件夾下所有文件編碼的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下
PHP轉(zhuǎn)換文件夾下所有文件的編碼 適合發(fā)布網(wǎng)站的其他編碼版本 比如你有一個GBK版本 你想有一個UTF8版本 或者你只有GBK的源碼 你想二次開發(fā) 但是你不想改變IDE的編碼方式 你可以用這個程序?qū)⑵渑哭D(zhuǎn)化為UTF8:
代碼如下:
<?php
/**
* 把一個文件夾里的文件全部轉(zhuǎn)碼 只能轉(zhuǎn)一次 否則全部變亂碼
* @param string $filename
*/
function iconv_file($filename,$input_encoding='gbk',$output_encoding='utf-8')
{
if(file_exists($filename))
{
if(is_dir($filename))
{
foreach (glob("$filename/*") as $key=>$value)
{
iconv_file($value);
}
}
else
{
$contents_before = file_get_contents($filename);
/*$encoding = mb_detect_encoding($contents_before,array('CP936','ASCII','GBK','GB2312','UTF-8'));
echo $encoding;
if($encoding=='UTF-8') mb_detect_encoding函數(shù)不工作
{
return;
}*/
$contents_after = iconv($input_encoding,$output_encoding,$contents_before);
file_put_contents($filename, $contents_after);
}
}
else
{
echo '參數(shù)錯誤';
return false;
}
}
iconv_file('./test');
?>
代碼如下:
復(fù)制代碼 代碼如下:
<?php
/**
* 把一個文件夾里的文件全部轉(zhuǎn)碼 只能轉(zhuǎn)一次 否則全部變亂碼
* @param string $filename
*/
function iconv_file($filename,$input_encoding='gbk',$output_encoding='utf-8')
{
if(file_exists($filename))
{
if(is_dir($filename))
{
foreach (glob("$filename/*") as $key=>$value)
{
iconv_file($value);
}
}
else
{
$contents_before = file_get_contents($filename);
/*$encoding = mb_detect_encoding($contents_before,array('CP936','ASCII','GBK','GB2312','UTF-8'));
echo $encoding;
if($encoding=='UTF-8') mb_detect_encoding函數(shù)不工作
{
return;
}*/
$contents_after = iconv($input_encoding,$output_encoding,$contents_before);
file_put_contents($filename, $contents_after);
}
}
else
{
echo '參數(shù)錯誤';
return false;
}
}
iconv_file('./test');
?>
您可能感興趣的文章:
- php使用遞歸計算文件夾大小
- PHP使用glob函數(shù)遍歷目錄或文件夾的方法
- php清空(刪除)指定目錄下的文件,不刪除目錄文件夾的實現(xiàn)代碼
- php的mkdir()函數(shù)創(chuàng)建文件夾比較安全的權(quán)限設(shè)置方法
- php遍歷文件夾下的所有文件和子文件夾示例
- php遍歷目錄與文件夾的多種方法詳解
- php文件夾與文件目錄操作函數(shù)介紹
- 探討PHP刪除文件夾的三種方法
- 使用PHP遍歷文件夾與子目錄的函數(shù)代碼
- php 遍歷顯示文件夾下所有目錄、所有文件的函數(shù),沒有分頁的代碼
- PHP文件及文件夾操作之創(chuàng)建、刪除、移動、復(fù)制
相關(guān)文章
php swoole多進程/多線程用法示例【基于php7nts版】
這篇文章主要介紹了php swoole多進程/多線程用法,結(jié)合實例形式分析了基于php7nts版使用swoole進行多線程創(chuàng)建、調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
PHP 獲取文件路徑(靈活應(yīng)用__FILE__)
__FILE__ ,是返回文件的完整路徑和文件名。如果用在包含文件中,則返回包含文件名。自 PHP 4.0.2 起,__FILE__ 總是包含一個絕對路徑,而在此之前的版本有時會包含一個相對路徑2013-02-02
PHP微信網(wǎng)頁授權(quán)的配置文件操作分析
這篇文章主要介紹了PHP微信網(wǎng)頁授權(quán)的配置文件操作,結(jié)合實例形式分析了php微信授權(quán)配置文件的相關(guān)功能、用法及操作注意事項,需要的朋友可以參考下2019-05-05
探討PHP使用eAccelerator的API開發(fā)詳解
本篇文章是對PHP使用eAccelerator的API開發(fā)進行了詳細的分析介紹,需要的朋友參考下2013-06-06

