Minifying final HTML output using regular expressions with CodeIgniter

后端 未结 3 1953
忘掉有多难
忘掉有多难 2020-12-04 13:56

Google pages suggest you to minify HTML, that is, remove all the unnecessary spaces. CodeIgniter does have the feature of giziping output or it can be done via .htacce

3条回答
  •  执念已碎
    2020-12-04 14:27

    For those curious about how Alan Moore's regex works (and yes, it does work), I've taken the liberty of commented it so it can be read by mere mortals:

    function process_data_alan($text) // 
    {
        $re = '%# Collapse ws everywhere but in blacklisted elements.
            (?>             # Match all whitespans other than single space.
              [^\S ]\s*     # Either one [\t\r\n\f\v] and zero or more ws,
            | \s{2,}        # or two or more consecutive-any-whitespace.
            ) # Note: The remaining regex consumes no text at all...
            (?=             # Ensure we are not in a blacklist tag.
              (?:           # Begin (unnecessary) group.
                (?:         # Zero or more of...
                  [^<]++    # Either one or more non-"<"
                | <         # or a < starting a non-blacklist tag.
                  (?!/?(?:textarea|pre)\b)
                )*+         # (This could be "unroll-the-loop"ified.)
              )             # End (unnecessary) group.
              (?:           # Begin alternation group.
                <           # Either a blacklist start tag.
                (?>textarea|pre)\b
              | \z          # or end of file.
              )             # End alternation group.
            )  # If we made it here, we are not in a blacklist tag.
            %ix';
        $text = preg_replace($re, " ", $text);
        return $text;
    }
    

    I'm new around here, but I can see right off that Alan is quite good at regex. I would only add the following suggestions.

    1. There is an unnecessary capture group which can be removed.
    2. Although the OP did not say so, the
提交回复
热议问题