PHP Snippet - Proper UTF-8 And GZip With Buffers-Handling And Content-Length Header And Ratio Information

Posted at

  1. first, set up internal engine to UTF-8.

    setlocale(LC_ALL, "he_IL.UTF-8");

    1. start the GZIP buffer

    2. start the internal-engine-buffer (now set for UTF-8).

    3. notice that the implicit flush is set twice, keep it.


  2. use your PHP script as usual,
    echo content, print, print_r, var_dump, etc..

    for example:
    echo str_repeat("-=á黪ğřƳȪȭDZȌȢɸʌΈΣϣொᆕᶙṕℳℱⅧ⅜⇦∬⓲┲▶➂ⶴは㌵㠩דוזחטש", 3000);

  3. script end:
    we will close the buffers and flush them, in a backward-order

    this will enable us to collect data on the each of the output's-length too!
    (I found it so interesting I will actually write some extra headers with the information..)

    1. (optionally)store size-of the raw output.

    2. close and flush the raw output.

    3. (optionally)store size-of the mb (UTF-8) buffered output.

    4. close and flush the mb_outputhandler (UTF-8) buffered output.

    5. (optionally)store size-of the mb (UTF-8) buffered output.

    6. close and flush the ob_gzhandler (GZIP) buffered output.

    $content_length__raw = ob_get_length();
    ob_end_flush(); //close the raw output which will be dumped into the internal engine buffer.
    $content_length__utf8 = ob_get_length();
    ob_end_flush(); //close the internal-engine buffer, which will be dumped into the gzip engine buffer.
    $content_length__gzip = ob_get_length();

  4. at this point the value of content_length__gzip is actually the Content-Length header value since its the real output length (the final layer).

  5. write the Content-Length header.
    header('Content-Length: ' . $content_length__gzip);

  6. any additional headers should be written now.
    for example:
    header('Content-Type: text/html; charset=utf-8');