傅令江的光影色彩世界
留住记忆的点滴
  • 首页
  • 文学
    • 诗词鉴赏
    • 美文共读
    • 原创
  • 编程
    • php
    • asp
    • .net
    • VB
    • C/C++
    • 易语言
    • js
    • 其他
    • 逆向
  • 运维
    • windows
    • linux
  • 光影色彩
    • 电影
    • 音乐
  • 科技
    • 互联网
    • 手机
  • 生活
    • 情感
  • 微语
10月52014

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

作者:令狐江   发布:2014-10-5 7:15   分类:php   阅读:3070次   评论:0条  
<?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 />';
	}
?> 
 



本文固定链接: https://www.fulingjiang.cn/php/67.html

blogger
该日志由 令狐江 于2014-10-5 7:15 Sunday发表在 php 分类下。
版权所有:《傅令江的光影色彩世界》 → 《PHP获取页面的JS和CSS的总数和文件》;
除特别标注,本博客所有文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签:
上一篇::php采集文章过滤版权信息
下一篇:使用Snoopy来获取某网页的所有链接

热门文章

  • 兄弟二周年祭

相关文章

  • 一个轻量级php家用爬虫
  • 通过模板替换生成html文件,mvp其实效率不高的,一个人开发原生最快
  • PHP读取文件内容的五种方式
  • PHP自动定时循环执行任务
  • php多个图片合成一张图片
取消回复

发表评论

亲,头像对么?

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


  • 日历

  • 存档

    • 2024年10月(1)
    • 2023年2月(1)
    • 2022年11月(1)
    • 2022年10月(10)
    • 2022年9月(13)
    • 2022年8月(2)
    • 2022年7月(14)
    • 2022年6月(2)
    • 2022年5月(8)
    • 2022年4月(7)
    • 2022年3月(13)
    • 2022年2月(2)
    • 2022年1月(9)
    • 2021年12月(2)
    • 2021年11月(4)
    • 2021年10月(2)
    • 2021年9月(6)
    • 2021年7月(4)
    • 2021年6月(3)
    • 2021年5月(3)
    • 2021年4月(11)
    • 2021年3月(13)
    • 2021年2月(2)
    • 2021年1月(1)
    • 2020年12月(1)
    • 2020年4月(5)
    • 2019年9月(1)
    • 2019年8月(1)
    • 2019年5月(3)
    • 2018年3月(1)
    • 2017年10月(1)
    • 2016年7月(1)
    • 2016年4月(1)
    • 2015年12月(1)
    • 2015年11月(3)
    • 2015年9月(1)
    • 2015年8月(10)
    • 2015年7月(1)
    • 2015年6月(1)
    • 2015年4月(1)
    • 2015年3月(3)
    • 2015年2月(8)
    • 2015年1月(4)
    • 2014年12月(1)
    • 2014年11月(27)
    • 2014年10月(13)
    • 2014年9月(14)
    • 2014年8月(26)
    • 2014年7月(21)
  • 最新评论

    • 令狐江:
      喜欢这首歌是因为可以引起共鸣!
  • 链接

    • 演讲稿网
    • Recollect
    • 演讲稿
    • 祁阳人生活网
    • 我爱演讲稿网
  • 搜索

  • 标签

      函数 自定义方法 SEO 分页 分页函数 分页方法 nginx重新的一些规则
  • 分类

    • 文学(0)
    • 编程(0)
    • 运维(0)
    • 光影色彩(0)
    • 科技(0)
    • 生活(0)
    • 诗词鉴赏(3)
    • 美文共读(1)
    • 原创(10)
    • php(111)
    • asp(1)
    • .net(0)
    • VB(0)
    • C/C++(0)
    • 易语言(0)
    • js(8)
    • 其他(9)
    • 逆向(2)
    • windows(11)
    • linux(121)
    • 电影(0)
    • 音乐(1)
    • 互联网(4)
    • 手机(0)
    • 情感(2)
  • 最新文章热门文章随机文章

    • 兄弟二周年祭
    • openai给的ionCube 解密代码,应该是老版本可以这样
    • WordPress – 5秒盾防CC(PHP通用代码)
    • 我高中最好的朋友今天猝死了-伤心得不行
    • Linux系统中 systemd-journaldCPU占用异常的解决方法
    • SVN Skipped 'xxx' -- Node remains in conflict 错误的解决办法
    • 解决Linux读写nfs共享盘速度慢的问题
    • php 获取302跳转后的地址
    • 让vsftp显示隐藏文件的办法,比如显示 .htaccess
    • 添加自签名https证书到centos系统信任的问题
    • php简单通用的分页类
    • php生成缩略图的类
    • php分页类,简单实用的一个
    • php操作mysql的一个类
    • php图片正则|preg_math|<img>标签
Copyright © 2001-2025 傅令江的光影色彩世界. Powered by www.fulingjiang.cn ICP备案:京ICP备14015190号-5