一个简单的正则表达式搜索和替换在php中的缩小/压缩javascript? [英] A simple regex search and replacement in php for minifying/compressing javascript?

查看:125
本文介绍了一个简单的正则表达式搜索和替换在php中的缩小/压缩javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在php中发布正则表达式搜索和替换,用于缩小/压缩JavaScript吗?

Can you post a regex search and replacement in php for minifying/compressing javascript?

例如,下面是一个简单的CSS

For example, here's a simple one for CSS

  header('Content-type: text/css');
  ob_start("compress");
  function compress($buffer) {
    /* remove comments */
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    /* remove tabs, spaces, newlines, etc. */
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    return $buffer;
  }

  /* put CSS here */

  ob_end_flush();

这里有一个html:

<?php
/* Minify All Output - based on the search and replace regexes. */
function sanitize_output($buffer)
{
    $search = array(
        '/\>[^\S ]+/s', //strip whitespaces after tags, except space
        '/[^\S ]+\</s', //strip whitespaces before tags, except space
        '/(\s)+/s'  // shorten multiple whitespace sequences
        );
    $replace = array(
        '>',
        '<',
        '\\1'
        );
  $buffer = preg_replace($search, $replace, $buffer);
    return $buffer;
}
ob_start("sanitize_output");
?>
<html>...</html>

但是关于javascript的一个?

推荐答案

用于缩小/压缩javascript的简单正则表达式不可能存在于任何地方。这可能有几个好的原因,但这里有以下几个原因:

A simple regex for minifying/compressing javascript is unlikely to exist anywhere. There are probably several good reasons for this, but here are a couple of these reasons:

换行符和分号
好​​javascript minifiers删除所有额外的换行符,但是因为javascript引擎将在每个语句结束时不使用分号,所以一个小型程序可以轻松地打破这个代码,除非它足够复杂以观察和处理不同的编码风格。

Line breaks and semicolons Good javascript minifiers remove all extra line breaks, but because javascript engines will work without semicolons at the end of each statement, a minifier could easily break this code unless it is sophisticated enough to watch for and handle different coding styles.

动态语言构造
许多好的javascript minifiers也会更改变量和函数的名称以缩小代码。例如,在您的文件中调用12次的名为strip_white_space的函数可能会被重命名为简单的'a',以节省您的缩减代码中的192个字符。除非您的文件具有很多的注释和/或空格,所以这些优化都是来自大部分文件大小的节省。

Dynamic Language Constructs Many of the good javascript minifiers available will also change the names of your variables and functions to minify the code. For instance, a function named 'strip_white_space' that is called 12 times in your file might be renamed simple 'a', for a savings of 192 characters in your minified code. Unless your file has a lot of comments and/or whitespace, optimizations like these are where the majority of your filesize savings will come from.

不幸的是,这比简单的正则表达式应该尝试处理更复杂。假设你做一些简单的事情:

Unfortunately, this is much more complicated than a simple regex should try to handle. Say you do something as simple as:

var length = 12, height = 15;
    // other code that uses these length and height values

var arr = [1, 2, 3, 4];
for (i = (arr.length - 1); i >= 0; --i) {
    //loop code
}

这是所有有效的代码。但是,如何更新minifier知道什么?第一个长度在它之前有var(但它不必),但height只有一个逗号。如果minifier足够聪明地替换第一个length,那么当用作数组的属性时,知道不改变单词length有多聪明?如果你定义了一个javascript对象,你特意定义了一个length属性,并用相同的点符号引用它,它会变得更复杂。

This is all valid code. BUT, how does the minifier know what to replace? The first "length" has "var" before it (but it doesn't have to), but "height" just has a comma before it. And if the minifier is smart enough to replace the first "length" properly, how smart does it have to be know NOT to change the word "length" when used as a property of the array? It would get even more complicated if you defined a javascript object where you specifically defined a "length" property and referred to it with the same dot-notation.

非正则表达式选项有几个项目使用更复杂的解决方案来解决这个问题,而不仅仅是一个简单的正则表达式,但是许多项目没有尝试更改变量名称,所以我仍然坚持使用Dean Edwards的packer或者Douglas Crockford的缩影器或类似于YUI Compressor。

Non-regex Options Several projects exist to solve this problem using more complex solutions than just a simple regex, but many of them don't make any attempt to change variable names, so I still stick with Dean Edwards' packer or Douglas Crockford's minifier or something like the YUI Compressor.

https://github.com/rgrove/jsmin-php/blob/master/jsmin.php

http://code.google.com/p/minify/

这篇关于一个简单的正则表达式搜索和替换在php中的缩小/压缩javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆