10月52014
PHP获取页面的JS和CSS的总数和文件
<?php
/**
* 获取页面所有的js 和 css 总数 和文件 *
* @param str $url 页面url
* @return array
* css_total css总数
* css_array css数组
* js_total js总数
* js_array js数组
*/
function count_css_js($url) {
$httptype = function_exists('curl_init');
if (!$httptype) {
$html = file_get_contents($url);
} else {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
echo "cURL Error: " . curl_error($ch);
}
}
$search = '/<link[^>]*?href="([^"]*)"[^>]*?type="text\/css"[^>]*?|<link[^>]*?type="text\/css"[^>]*?href="([^"]*)"[^>]*?>/is';
preg_match_all($search, $html , $r1);
$css_array = array_filter(array_merge($r1[1], $r1[2]));
$css_total = count($css_array);
foreach ($css_array as $k => $v) {
$isasr = stripos($v, 'http://');
if ($isasr != 0 || $isasr === false) {
$css_array[$k] = _expandlinks($v, $url); //相对地址转绝对地址
}
}
$search = '/<script[^>]*?src="([^"]*)"[^>]*?>/is';
preg_match_all($search, $html , $r2);
$js_array = $r2[1];
foreach ($js_array as $k => $v) {
$isasr = stripos($v, 'http://');
if ($isasr != 0 || $isasr === false) {
$js_array[$k] = _expandlinks($v, $url);
}
}
$js_total = count($r2[1]);
return array('css_total' => $css_total, 'css_array' => $css_array, 'js_total' => $js_total, 'js_array' => $js_array);
}
/**
* 获取真实地址
*
* @param str $hosturl 输入url
* @return str 返回真实url
*/
function realurl($hosturl) {
$header = get_headers($hosturl, 1);
$righturl = '';
if (strstr($header[0], '302') || strstr($header[0], '301')) {
$righturl = (strpos($header['Location'], '/') == 0) ? $hosturl . $header['Location']: $header['Location'];
} elseif (strstr($header[0], '200') || $header[0] == 'HTTP/1.1 200 OK' || $header[0] == 'HTTP/1.0 200 OK') {
$righturl = $hosturl;
} else {
return false;
}
if (is_array($righturl)) {
$righturl = $righturl[0];
return $righturl;
} else {
return $righturl;
}
}
/**
* 将相对地址转绝对
*
* @param str $Links 链接地址
* @param str $URI 主机地址
*/
function _expandlinks($links, $URI) {
$URI_PARTS = parse_url($URI);
$host = $URI_PARTS["host"];
preg_match("/^[^\?]+/", $URI, $match);
$match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|", "", $match[0]);
$match = preg_replace("|/$|", "", $match);
$match_part = parse_url($match);
$match_root = $match_part["scheme"] . "://" . $match_part["host"];
$search = array(
"|^http://" . preg_quote($host) . "|i",
"|^(\/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/\./|",
"|/[^\/]+/\.\./|"
);
$replace = array(
"",
$match_root . "/",
$match . "/",
"/",
"/"
);
$expandedLinks = preg_replace($search, $replace, $links);
return $expandedLinks;
}
?>
<?php
//以下是测试
$url = 'http://fulingjiang.com/';
$url = realurl($url);
$r = count_css_js($url);
echo 'css总数:' . $r['css_total'] . '<br />';
foreach ($r['css_array'] as $v) {
echo $v . '<br />';
}
echo '<br />';
echo 'js总数:' . $r['js_total'] . '<br />';
foreach ($r['js_array'] as $v) {
echo $v . '<br />';
}
?>
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!