利用正则为css和js链接增加版本信息

时间:2014-02-15所属栏目: PHP/PHP框架 来源: 未知 热度:

$html = <<< HTML
<style type="text/css">import url("/css.css?a=1");</style> 
<link rel="stylesheet" type="text/css href="http://s.8591.com.tw/css/main.css"" media="all">
<link href="http://s.8591.com.tw/css/index.css" type="text/css" rel="stylesheet" media="all">
<script type='text/javascript' src='/js/jquery/jquery.js'></script>
<script src='/js/jquery/colorbox/min.js?v=1329959498' type='text/javascript'></script>
HTML;

$ver    = '1.0.3';
$p      = "/(<style.*?url\([\'\"]|<(?:link|script).*?(?:href|src)=[\"\'])([^\'\"]+)/i";
$html   = preg_replace_callback($p, create_function('$m', 'return $m[1].$m[2].(strpos($m[2], "?")?"&":"?")."_v='.$ver.'";'), $html);
echo $html;

执行结果

<style type="text/css">import url("/css.css?a=1&_v=1.0.3");</style> 
<link rel="stylesheet" type="text/css href="http://s.8591.com.tw/css/main.css?_v=1.0.3"" media="all">
<link href="http://s.8591.com.tw/css/index.css?_v=1.0.3" type="text/css" rel="stylesheet" media="all">
<script type='text/javascript' src='/js/jquery/jquery.js?_v=1.0.3'></script>
<script src='/js/jquery/colorbox/min.js?v=1329959498&_v=1.0.3' type='text/javascript'></script>
标签:
  • 上一篇:没有了
  • 下一篇:没有了
相关文章
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)