首頁 後端開發 php教程 壓縮多個CSS與JS檔案的php程式碼

壓縮多個CSS與JS檔案的php程式碼

Jul 25, 2016 am 09:04 AM

  1. header('Content-type: text/css');
  2. ob_start("compress");
  3. function compress( $buffer) {
  4. /* remove comments */
  5. $buffer = preg_replace('!/*[^*]**+([^/][^*]**+)*/!', '', $buffer);
  6. /* remove tabs, spaces, newlines, etc. */
  7. $buffer = str_replace(array("rn", "r", "n", "t", ' ', ' ', ' '), '', $buffer);
  8. return $buffer;
  9. }
  10. /* your css files */
  11. include('galleria.css' );
  12. include('articles.css');
  13. ob_end_flush();
  14. ?>
複製代碼

實例化: test.php

  1. test
複製程式碼

2. 壓縮js 利用jsmin類 資料來源:http://code.google.com/p/minify/ compress.php

  1. header('Content-type: text/javascript');
  2. require 'jsmin.php';
  3. echo JSMin::minify(file_get_contents('common.js') . file_get_contents('common2.js'));
  4. ?>
複製程式碼
複製程式碼複製程式碼複製程式碼複製程式碼複製碼

common.js 警報('第一個js');

common.js alert('第二個js');

jsmin.php

  1. /**
  2. * jsmin.php - Douglas Crockford 的 JSMin 的擴充 PHP 實作。
  3. *
  4. * ;
  5. * $minifiedJs = JSMin::minify($js);
  6. * 程式碼>;
  7. *
  8. * 這是jsmin.c到PHP 的直接移植,進行了一些PHP 性能調整和
  9. * 修改以保留一些註釋(見下文)。此外,JSMin::minify() 不使用
  10. * stdin/stdout,而是接受一個字串作為輸入並傳回另一個
  11. * 字串作為輸出。
  12. *
  13. * 保留包含 IE 條件編譯的註釋,多行也是如此
  14. * 以「/*!」開頭的註釋(用於文件目的)。在後一種情況下
  15. * 在註解周圍插入換行符號以增強可讀性。
  16. *
  17. * 需要 PHP 5 或更高版本。
  18. *
  19. * 特此授予在
  20. * 與jsmin.c 相同的條款下使用此版本庫的權限,它具有以下許可證:
  21. *
  22. * --
  23. *版權所有(c) 2002 Douglas Crockford (www.crockford.com)
  24. *
  25. * 特此向取得
  26. * 本軟體及相關文件文件副本的任何人免費授予許可(「軟體」),不受限地經營
  27. *軟體,包括但不限於
  28. *使用、複製、修改、合併、發布、分發、再授權和/或銷售副本 * 軟體,並允許向其提供軟體的人員
  29. * 這樣做,但須滿足以下條件:
  30. *
  31. * 上述版權聲明和本許可聲明應包含在軟體的所有
  32. * 副本或大部分。
  33. *
  34. * 本軟體應用於善良,而非邪惡。
  35. *
  36. * 本軟體以「現況」提供,不提供任何形式的明示或
  37. * 暗示保證,包括但不限於適銷性保證,
  38. * 針對特定用途的適用性和非侵權。在任何情況下,
  39. * 作者或版權持有者均不對任何索賠、損害或其他
  40. * 責任負責,無論是合約、侵權或其他行為,由
  41. * 引起或由與軟體的連接或
  42. * 軟體中的使用或其他交易。
  43. * --
  44. *
  45. * @package JSMin
  46. * @author Ryan Grove (PHP 埠)
  47. * @author Steve Clay (修改+ 清理)
  48. * @author Andrea Giammarchi (spaceBeforeRegExp)
  49. * @copyright 2002 Douglas Crockford (jsmin.c)
  50. * @copyright 2008 Ryan Grove (PHP 連接埠)
  51. * @license http://opensource.org/licenses/mit-license.php MIT 授權
  52. * @link http ://code.google.com/p/jsmin-php/
  53. */
  54. class JSMin {
  55. const ORD_LF = 10; ORD_SPACE = 32;
  56. const ACTION_KEEP_A = 1;
  57. const ACTION_DELETE_A = 2;
  58. const ACTION_DELETE_A_B = 3;
  59. protected $ = '';
  60. protected $input = '';
  61. 受保護的$inputIndex = 0;
  62. 受保護的$inputLength = 0;
  63. protected $lookAhead = null; protec output = '';
  64. /**
  65. * 縮小 Javascript
  66. *
  67. * @param string $js 要縮小的 Javascript
  68. * @return string
  69. */
  70. public static function minify($js)
  71. {
  72. // 留意像「++ +」和「- + +」這樣的語法
  73. $p = '\+';
  74. $m = '\-';
  75. if (preg_match("/([$p$m])(?:\1 [ $p$m]| (?:$p$p|$m$m))/", $js)) {
  76. // 可能是預先縮小的,並且會被JSMin
  77. 破壞return $js ;
  78. }
  79. $jsmin = new JSMin($js);
  80. return $jsmin->min();
  81. }
  82. /*
  83. * 不要建立JSMin 實例,而是使用靜態函數minify,
  84. * 檢查mb_string 函數重載並避免錯誤
  85. * 嘗試重新縮小閉包編譯器的輸出
  86. *
  87. * @private
  88. */
  89. public function __construct($input)
  90. {
  91. $this->input = $input;
  92. }
  93. /**
  94. * 執行縮小,回傳結果
  95. */
  96. public function min()
  97. {
  98. if ($this->output !== '') { // min 已經運作
  99. 回傳$this-> 輸出;
  100. }
  101. $mbIntEnc = null;
  102. if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2) ) {
  103. $mbIntEnc = mb_internal_encoding();
  104. mb_internal_encoding('8bit');
  105. }
  106. $this->input = str_replace("rn", "n", $🎜> $this->input = str_replace("rn", "n", $🎜> $this->input = str_replace("rn", "n", $🎜> );
  107. $this->inputLength = strlen($this->input);
  108. $this->action(self::ACTION_DELETE_A_B);
  109. while ($this-> a !== null) {
  110. // 決定下一個指令
  111. $command = self::ACTION_KEEP_A; // 預設
  112. if ($this->a === ' ') {
  113. if (! $this->isAlphaNum($this->b)) {
  114. $command = self ::ACTION_DELETE_A;
  115. }
  116. } elseif ($this->a === "n") {
  117. if ($this->b === ' ') {
  118. $command = self: :ACTION_DELETE_A_B;
  119. // 如果是mbstring.func_overload & 2,必須檢查null b,
  120. // 否則mb_strpos 會給予警告
  121. } elseif ($this->b === null
  122. | | (false === strpos('{[(+-', $this->b)
  123. && ! $this->isAlphaNum($this->b))) {
  124. $command = self::ACTION_DELETE_A;
  125. }
  126. } elseif (! $this->isAlphaNum($this- >a)) {
  127. if ($this->b === ' '
  128. || ($this->b === "n"
  129. && (false === strpos('} ])+-"'', $this->a)))) {
  130. $command = self::ACTION_DELETE_A_B;
  131. }
  132. }
  133. $this->action($command);
  134. }
  135. $this->output = trim($this -> 輸出);
  136. if ($mbIntEnc !== null) {
  137. mb_internal_encoding($mbIntEnc);
  138. }
  139. return $this->output;
  140. }
  141. /**
  142. * ACTION_KEEP_A = 輸出 A。將 B 複製到 A。獲取下一個 B。
  143. * ACTION_DELETE_A = 將 B 複製到 A。獲取下一個 B。
  144. * ACTION_DELETE_A_B = 取得下一個 B。
  145. */
  146. 受保護函數運算($command)
  147. {
  148. switch ($ command) {
  149. case self::ACTION_KEEP_A:
  150. $this->output .= $this->a;
  151. // 失敗
  152. case self::ACTION_DELETE_A:
  153. $this-> a = $this->b;
  154. if ($this->a === "'" || $this->a === '"') { // 字串文字
  155. $str = $this->a; // 如果需要異常
  156. while (true) {
  157. $this->output .= $this->a
  158. $this->a = $this->get( ) ;
  159. if ($this->a === $this->b) { // 結束引號
  160. break; }
  161. if (ord($this->a ) throw new JSMin_UntermeratedStringException(
  162. "JSMin: 字節處未終止的字串"
  163. . $this->inputIndex . ": {$str}"); > }
  164. $str .= $this->a
  165. if ($this->a === '\') {
  166. $this->output .= $this->; ;一個;
  167. $this-> a = $this->get();
  168. $str .= $this->a;
  169. }
  170. }
  171. }
  172. // 失敗
  173. case self::ACTION_DELETE_A_B:
  174. $this->b = $this->next();
  175. if ($this->b === '/' && $ this->isRegexpLiteral()) { // 正規表示式文字
  176. $this->output .= $this->a . $這個->b;
  177. $pattern = '/'; // 如果需要異常
  178. while (true) {
  179. $this->a = $this->get();
  180. $pattern .= $this->a;
  181. if ($this->a = == '/') { // 結束模式
  182. break; // while (true)
  183. } elseif ($this->a === '\') {
  184. $this->output . = $this->a;
  185. $this->a = $this->get();
  186. $pattern .= $this->a;
  187. } elseif (ord($this->a ) throw new JSMin_UntermeratedRegExpException(
  188. "JSMin: 位元組處未終止正規表示式"
  189. . $this-> ;inputIndex .": {$pattern}") ;
  190. }
  191. $this->輸出.= $this->a;
  192. }
  193. $this->b = $this->next();
  194. }
  195. / / 結束狀況ACTION_DELETE_A_B
  196. }
  197. }
  198. protected function isRegexpLiteral()
  199. {
  200. if (false !== strpos("n{;| ?", $this->a)) { // 我們沒有除
  201. return true;
  202. }
  203. if (' ' === $this- >a) {
  204. $length = strlen ($this->output);
  205. if ($length return true; > // 不能分割關鍵字
  206. if (preg_match('/(?: case|else|in|return|typeof)$/', $this->output, $m)) {
  207. if ($this->output === $m[0]) { // 奇怪但可能發生
  208. return true
  209. }
  210. // 確保它是一個關鍵字,而不是結尾標識符
  211. $charBeforeKeyword = substr($this->output, $length - strlen($m[0 ]) - 1, 1);
  212. if (! $this->isAlphaNum($charBeforeKeyword)) {
  213. return true;
  214. }
  215. }
  216. }
  217. 回傳false; 🎜> }
  218. /**
  219. * 取得下一個字元。將 ctrl 字元轉換為空格。
  220. */
  221. 受保護函數get()
  222. {
  223. $c = $this->lookAhead;
  224. $this->lookAhead = null;
  225. if ($c === null) {
  226. if ($this->inputIndex inputLength) {
  227. $c = $this->input[$this- >輸入索引];
  228. $this->inputIndex += 1;
  229. } else {
  230. 回傳null;
  231. }
  232. }
  233. if ($c === "r" | | $c === "n") {
  234. return "n";
  235. }
  236. if (ord($c) return ' ' ;
  237. }
  238. 回傳$c;
  239. }
  240. /**
  241. * 取得下一個字元。如果是 ctrl 字符,則轉換為空格或換行符。
  242. */
  243. 受保護函數peek()
  244. {
  245. $this-> lookAhead = $this->get();
  246. return $this->lookAhead;
  247. }
  248. /**
  249. * $c 是字母、數字、底線、美元符號、轉義符還是非 ASCII?
  250. */
  251. protected function isAlphaNum($c)
  252. {
  253. return (preg_match('/^[0-9a-zA-Z_) \$\\]$/', $c) || ord($c) > 126);
  254. }
  255. 受保護函數singleLineComment()
  256. {
  257. $comment = '';
  258. while (true) {
  259. $get = $this->get();
  260. $comment .= $get;
  261. if (ord($get) // if IE 條件註解
  262. if (preg_match('/^\/@ (?:cc_on| if|elif|else|end)\b/', $comment)) {
  263. return "/{$comment}";
  264. }
  265. return $get;
  266. }
  267. }
  268. }
  269. protected function multipleLineComment()
  270. {
  271. $this-this>( ) ;
  272. $comment = '';
  273. while (true) {
  274. $get = $this->get();
  275. if ($get === '*') {
  276. if ($this->peek() === '/') { // 到達註解結尾
  277. $this->get();
  278. // if 註解被YUI 壓縮器保留
  279. if ( 0 === strpos($comment, '!')) {
  280. return "n/*" . substr($comment, 1) "*/n";
  281. }
  282. // if IE 條件註解
  283. if (preg_match('/^@(?:cc_on|if|elif|else|end)\b/', $comment)) {
  284. return "/*{$comment }*/";
  285. }
  286. return ' ';
  287. }
  288. } elseif ($get === null) {
  289. throw new JSMin_UntermeratedCommentException(
  290. "JSMin: 字節處未終止註解"
  291. "JSMin: 字節處未終止註解"
  292. . $this->inputIndex . ": / *{$評論}");
  293. }
  294. $comment .= $get;
  295. }
  296. }
  297. /* *
  298. * 取得下一個字符,跳過註釋。
  299. * 有些評論可能會被保留。
  300. */
  301. protected function next()
  302. {
  303. $get = $this->get();
  304. if ($get !== '/') {
  305. return $get;
  306. }
  307. switch ($this->peek()) {
  308. case '/': return $this->singleLineComment();
  309. case '*': return $this ->multipleLineComment();
  310. 預設:回傳$get;
  311. }
  312. }
  313. }
  314. 類別JSMin_UntermeratedStringException 擴展了異常擴展了
  315. 類別JSMin_UntermminateRegExpException 擴充了異常{}
  316. ?>
複製程式碼

呼叫範例:

複製程式碼


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

會話如何劫持工作,如何在PHP中減輕它? 會話如何劫持工作,如何在PHP中減輕它? Apr 06, 2025 am 12:02 AM

會話劫持可以通過以下步驟實現:1.獲取會話ID,2.使用會話ID,3.保持會話活躍。在PHP中防範會話劫持的方法包括:1.使用session_regenerate_id()函數重新生成會話ID,2.通過數據庫存儲會話數據,3.確保所有會話數據通過HTTPS傳輸。

描述紮實的原則及其如何應用於PHP的開發。 描述紮實的原則及其如何應用於PHP的開發。 Apr 03, 2025 am 12:04 AM

SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。

在PHPStorm中如何進行CLI模式的調試? 在PHPStorm中如何進行CLI模式的調試? Apr 01, 2025 pm 02:57 PM

在PHPStorm中如何進行CLI模式的調試?在使用PHPStorm進行開發時,有時我們需要在命令行界面(CLI)模式下調試PHP�...

如何在系統重啟後自動設置unixsocket的權限? 如何在系統重啟後自動設置unixsocket的權限? Mar 31, 2025 pm 11:54 PM

如何在系統重啟後自動設置unixsocket的權限每次系統重啟後,我們都需要執行以下命令來修改unixsocket的權限:sudo...

解釋PHP中的晚期靜態綁定(靜態::)。 解釋PHP中的晚期靜態綁定(靜態::)。 Apr 03, 2025 am 12:04 AM

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章討論了框架中的基本安全功能,以防止漏洞,包括輸入驗證,身份驗證和常規更新。

See all articles