function relative_to_absolute($content,$protocol, $domain, $f_url) {
//根目录相对路径(如href="/a/b.html")转换
$new_content =preg_replace('/hrefs*=s*(['"])s*//','href=1'.$protocol.$domain.'/', $content);
$new_content =preg_replace('/srcs*=s*(['"])s*//', 'src=1'.$protocol.$domain.'/',$new_content);
//当前页相对路径(如href="a/b.html")转换
$new_content =preg_replace('/hrefs*=s*(['"])(?!(http|https)://)/','href=1'.$protocol.$domain.$f_url,$new_content);
$new_content =preg_replace('/srcs*=s*(['"])(?!(http|https)://)/','src=1'.$protocol.$domain.$f_url, $new_content);
return $new_content;
}
function SaveTo($filename)
{
$fp=@fopen($filename,"w") ordie("DedeTag Engine Create File False");
if(substr($_SERVER['PHP_SELF'],-6)=='_m.php'||substr($filename,-13)=='/m/index.html'){ //跳转适配站识别是否为移动端生成,不影响pc端的gbk编码。移动端为独立站点需去掉此判断条件。
$f_url=explode('www.域名.com/m',dirname($filename));//分割路径,获取当前页相对路径的目录部分
//如dirname($filename)得到的本地绝对路径为D:/wwwroot/www.域名.com/m/yygk/xwzx,用网站目录"www.域名.com/m"作为标识分割路径,得到目录部分"/yygk/xwzx"。
$html=$this->GetResult();
$html=$this->relative_to_absolute($html,'http://','m.域名.com',$f_url[1].'/');//相对路径转换绝对路径
$html=str_replace('<metacharset="gb2312">','<metacharset="utf-8">',iconv('gbk','utf-8//ignore',$html)); //转换为utf-8编码声明,fwrite会以此生成对应编码的静态页面
$html=str_replace('<a','<a target="_blank" ',$html); //<a>标签加target
$html=str_replace('<img','<mip-img ',$html); //替换<img>标签
/* 主要针对编辑器生成的内联样式,将内联样式转换到head的style标签中 */
if(preg_match_all('/sstyles*=s*['"](.*?)['"]/',$html,$css)){
$css0=array_unique($css[0]);//过滤重复style
foreach($css0as $k => $v){
$html=str_replace($v,'class="mip_add_css_'.$k.'"',$html); //mip_add_css_为自定义样式名前缀,可自行修改,但需避免与原有样式名重复
$temp_name='mip_add_css_'.$k;
$$temp_name=$css[1][$k];
$add_css.='.'.$temp_name.'{'.$css[1][$k]."}n";
}
$html=str_replace('<stylemip-custom>',"<style mip-custom>n".$add_css,$html);
}
fwrite($fp, $html);
}else{ //pc端执行
fwrite($fp,$this->GetResult());
}
fclose($fp);
}
|
版权声明: 本站资源均来自互联网或会员发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!
转载请注明: 织梦模板下载dede源码 gbk站点mip如何改造的教程