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

  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
使用數據庫存儲會話的優點是什麼?使用數據庫存儲會話的優點是什麼?Apr 24, 2025 am 12:16 AM

使用數據庫存儲會話的主要優勢包括持久性、可擴展性和安全性。 1.持久性:即使服務器重啟,會話數據也能保持不變。 2.可擴展性:適用於分佈式系統,確保會話數據在多服務器間同步。 3.安全性:數據庫提供加密存儲,保護敏感信息。

您如何在PHP中實現自定義會話處理?您如何在PHP中實現自定義會話處理?Apr 24, 2025 am 12:16 AM

在PHP中實現自定義會話處理可以通過實現SessionHandlerInterface接口來完成。具體步驟包括:1)創建實現SessionHandlerInterface的類,如CustomSessionHandler;2)重寫接口中的方法(如open,close,read,write,destroy,gc)來定義會話數據的生命週期和存儲方式;3)在PHP腳本中註冊自定義會話處理器並啟動會話。這樣可以將數據存儲在MySQL、Redis等介質中,提升性能、安全性和可擴展性。

什麼是會話ID?什麼是會話ID?Apr 24, 2025 am 12:13 AM

SessionID是網絡應用程序中用來跟踪用戶會話狀態的機制。 1.它是一個隨機生成的字符串,用於在用戶與服務器之間的多次交互中保持用戶的身份信息。 2.服務器生成並通過cookie或URL參數發送給客戶端,幫助在用戶的多次請求中識別和關聯這些請求。 3.生成通常使用隨機算法保證唯一性和不可預測性。 4.在實際開發中,可以使用內存數據庫如Redis來存儲session數據,提升性能和安全性。

您如何在無狀態環境(例如API)中處理會議?您如何在無狀態環境(例如API)中處理會議?Apr 24, 2025 am 12:12 AM

在無狀態環境如API中管理會話可以通過使用JWT或cookies來實現。 1.JWT適合無狀態和可擴展性,但大數據時體積大。 2.Cookies更傳統且易實現,但需謹慎配置以確保安全性。

您如何防止與會議有關的跨站點腳本(XSS)攻擊?您如何防止與會議有關的跨站點腳本(XSS)攻擊?Apr 23, 2025 am 12:16 AM

要保護應用免受與會話相關的XSS攻擊,需採取以下措施:1.設置HttpOnly和Secure標誌保護會話cookie。 2.對所有用戶輸入進行輸出編碼。 3.實施內容安全策略(CSP)限制腳本來源。通過這些策略,可以有效防護會話相關的XSS攻擊,確保用戶數據安全。

您如何優化PHP會話性能?您如何優化PHP會話性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显著提升应用在高并发环境下的效率。

什麼是session.gc_maxlifetime配置設置?什麼是session.gc_maxlifetime配置設置?Apr 23, 2025 am 12:10 AM

theSession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceisesneededeededeedeedeededto toavoidperformance andunununununexpectedLogOgouts.3)

您如何在PHP中配置會話名?您如何在PHP中配置會話名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函數配置會話名稱。具體步驟如下:1.使用session_name()函數設置會話名稱,例如session_name("my_session")。 2.在設置會話名稱後,調用session_start()啟動會話。配置會話名稱可以避免多應用間的會話數據衝突,並增強安全性,但需注意會話名稱的唯一性、安全性、長度和設置時機。

See all articles

熱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

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

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

記事本++7.3.1

記事本++7.3.1

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