-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetBody.php
31 lines (31 loc) · 1.04 KB
/
getBody.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
ob_start();//开启缓存
// $content= ob_get_contents();//从缓存中获取内容
$url= $_POST['url'];
$url= 'http://teach.ustb.edu.cn/';
$html=file_get_contents($url);
ob_end_clean();//关闭缓存并清空
/***缓存结束***/
// echo $content;
// echo "ok";
function getBody($html,$urlRoot = null){
//提取BODY主体
preg_match('/<!--body-->(.*?)<!--body-->/is ', $html, $bodyArr);
if(!$bodyArr){
preg_match('/<body.*?>(.*?)<\/body>/is ', $html, $bodyArr);
}
if(!$bodyArr){
echo "http请求失败";
exit;
}
$body = $bodyArr[1];
//替换img文件
$body = preg_replace('/(<[img|IMG].*src=[\'|"])(\.\.\/)*(img.[^\'||^"]+)/',"$1$urlRoot$3",$body);
//替换html文件内的css背景图片
$body = preg_replace('~\b(background(-image)?\s*:(.*?)\(\s*[\'|"]?)(\.\.\/)*(img.*?)?\s*\)~i',"$1$urlRoot$5)",$body);
return $body;
}
$body=getBody($html,$url);
echo $body;
file_put_contents('htmlContent.php', $body);
?>