<?php function addver($str,$version){ // $str: html file codes $head = '/\<head\>.*\<\/head\>/is'; $search = '/\.css\\s*(?=["\'])/i'; $replace = '.css?v='.$version; $search_js = '/\.js\\s*(?=["\'])/i'; $replace_js = '.js?v='.$version; preg_match($head ,$str,$matched); $tempStr = preg_replace($search, $replace, $matched[0]); $tempStr = preg_replace($search_js, $replace_js, $tempStr ); return preg_replace($head ,$tempStr, $str); } //test $str = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="../style/common.css "/> <link rel="stylesheet" type="text/css" href=\'../style/layout.CSS\'/> <link rel="stylesheet" type="text/css" href="../style/module.css"/> <script type="text/javascript" src="../script/jquery.min.js?v=1.3.2"></script> <script type="text/javascript" src="../script/jquery-ui.custom.min.js?v=1.7.3"></script> <script type="text/javascript" src="../script/commoned.js"></script> <script type="text/javascript" src="../script/base.js?v=0.0.1"></script> <script type="text/javascript" src="../script/apage.js"></script> <script type="text/javascript" src="../script/checklogin.js"></script> <script type="text/javascript" src="../script/pages/employee-add.js"></script> <title>test</title> </head> <body> <input id="tst" class="btn" type = "button" value = "test css and jq"/> <script> alert($("#tst").attr("id")); </script> <div style = "margin:100px;">.css" .js"</div> </body> </html> '; echo "<hr />"; echo addver($str,'1.0'); echo "<hr />"; ?>