教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP批量修改静态html文件编码的例子

PHP批量修改静态html文件编码的例子

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供PHP批量修改静态html文件编码的例子等资源,欢迎您收藏本站,我们将为您提供最新的PHP批量修改静态html文件编码的例子资源
一堆的文件编码都是ANSI的,本来想用linux shell批量修改文件编码,却不太熟悉,写了个PHP脚本批量修改html文件的编码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6583')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6583>

<?php
 
//1.遍历出所有html文件
$dir = './jquery/jquery/'; //文件目录、
 
$move_to = './jquery/jquery_new/';
 
foreach(glob($dir . '*.htm*') as $html)
{
    //读取文件
    $fp = fopen($html, 'a ');
    $html_string = fread($fp, filesize($html));
   
    //转码操作
    $html_string = str_replace('charset=gb2312', 'charset=UTF-8', $html_string);
   
    $file_code = mb_detect_encoding()($html_string, array('UTF-8','GBK','LATIN1','BIG5'));
   
    if($file_code = 'CP936')
    {
        $html_string = mb_convert_encoding($html_string ,'utf-8' , 'gbk');
    }
   
    //获取文件名
    $slice = explode('/', $html);
    $file_name = end($slice);
   
    //移到新文件
    file_put_contents($move_to . $file_name, $html_string);
   
    fclose($fp);
}
 
echo 'ok, 执行完成';

其实主要用到mb_detect_encoding检测编码;使用mb_convert_encoding转换编码;这是mbstring扩展的函数

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4240')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4240>


<?php
 
/**
 * 检测编码
 * string mb_detect_encoding ( string $str [, mixed $encoding_list = mb_detect_order() [, bool $strict = false ]] )
 *
 * @param $str 要检测的字符串
 * @param $encoding_list 检测编码的顺序
 * @param $strict 是否严格检测编码
 */
 
$str = 'http://www.jiaochengji.com-PHP点点通';
echo $file_code = mb_detect_encoding($str, array('UTF-8','GBK','LATIN1','BIG5')); //UTF-8
 
/**
 * 转换编码
 * string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )
 *
 * @param $str 要转换的字符串
 * @param $to_encoding 转换后的字符串
 * @param $from_encoding
 */
 
echo mb_convert_encoding($str, 'GBK', 'UTF-8');

您可能感兴趣的文章:
php静态变量的简单示例
php页面静态化的小例子
php中对static关键字的理解
php中static关键字的理解
PHP批量修改静态html文件编码的例子
linux批量修改文件名的shell脚本
php中static 静态变量和普通变量的区别
php怎么改成html
用LibTemplate实现静态网页的生成
PHP生成静态文件简单示例

[关闭]
~ ~