今天新写了了网页预览的接口,需要把原来网页的css js全部过滤掉,并且添加一个统一的css样式,功能实现并不复杂,但是还是花了不少时间。把代码贴过来。
public function preview(){
$url=$_GET['url'];
$url="http://192.168.0.112:8088/search/cache/?url=".$url;
$html = file_get_contents($url);
$encode = mb_detect_encoding($html, array('UTF-8','GB2312','GBK','BIG5','ASCII'));
//echo $encode;
//header("Content-Type: text/html; charset=UTF-8");
$basic_css='<head><style type="text/css">
body{line-height:1.3em;font-size:13px;}img{border:medium none;}form{margin:0;}.bc{background-color:#CCECFF;font-size:13px;}
.in{margin-bottom:5px;}
.c0{word-wrap:break-word;}.c1{margin:0;padding:0;}.c2{color:rgb(198, 10, 0);background-color:rgb(255, 255, 255);}.c3{font-weight:bold;}.c4{border-top:5px solid rgb(0, 0, 0);}.c5{word-break:break-all;padding-right:8px;vertical-align:top;display:inline-block;max-width:150px;}.c6{width:140px;}.c7{border:none;}.c8{word-break:break-all;padding-right:8px;vertical-align:top;display:inline-block;max-width:75px;}.c9{width:65px;}
.z{margin: 5px 0 5px 0;background-color:#E4EDF8;padding-left:3px}.z a{color:#554;font-size:14px;text-decoration:none;}
.nav2{color:#000000;background-color:#E1E1F9;}
.footer{margin-top:8px;padding-top:8px;clear:both;}
.footer a{color:#00e}
.tc_relate { margin-top: 19px; font-size: 15px;color:#039}
.tc_h_touch { height: 27px; line-height: 27px; border: 1px solid #C9C9C9;border-width: 1px 0;padding-left: 10px;background:#E4EDF8;line-height: 27px;font-size: 15px;}
.i_search { font-size: 15px; margin-bottom: 11px; }
.i_search { padding: 0 6px;}
#f_3 a{color:#039}
#f_2 a{color:#039}
</style>';
//echo $html;
$str = $html;
$str = preg_replace( "@<script(.*?)</script>@is", "", $str );
$str = preg_replace( "@<iframe(.*?)</iframe>@is", "", $str );
$str = preg_replace( "@<link(.*?)/>@is", "", $str );
$str = preg_replace( "@<style(.*?)</style>@is", "", $str );
$str = preg_replace( "@<head>@is", $basic_css, $str );
//$str = preg_replace( "@<(.*?)>@is", "", $str );
echo $str;
}
}