PHP获取页面的JS和CSS的总数和文件

我又写了个收罗php函数,可以获取JS和CSS的总数,有些的站长东西大概也有这个成果,好比百度的网站速度测试。我写这个小对象也留意到了一些细节问题,好比网站做了301、302重定向的问题,尚有相对绝对路径,大概还会其它的问题,不外已经完成了一个雏形。上源码:

<?php /** * 获取页面所有的js 和 css 总数 和文件 by enenba 2012-7-9 * * @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( "|^" . preg_quote($host) . "|i", "|^(\/)|i", "|^(?!)(?!mailto:)|i", "|/\./|", "|/[^\/]+/\.\./|" ); $replace = array( "", $match_root . "/", $match . "/", "/", "/" ); $expandedLinks = preg_replace($search, $replace, $links); return $expandedLinks; } ?> <?php //以下是测试 $url = 'http://enenba.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 />'; } ?>

下图为测试输出的功效

点击查察原图

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7827.html