Hi,

I was wondering how you can attach everything from a html source, so all the spaces, all the tabs, newlines are gone and everything is glued togheter?

I tried with implode, but that doesn't do anything because I can't get rid of the spaces, tabs, newlines etc. Is there an easy way to achieve this?

Dani AI

Generated

Short answer: capture the HTML output and run a safe minifier that protects blocks where whitespace or comments matter (script, style, pre, textarea) and preserves conditional comments. Naive methods that just implode strings or blindly strip spaces will break JS/CSS and some HTML constructs.

A simple, reliable pattern:

  1. Capture output with ob_start.
  2. Replace protected blocks (script/style/pre/textarea) with placeholders.
  3. Remove non‑conditional HTML comments, collapse whitespace between tags, and compress runs of spaces.
  4. Restore the protected blocks and send the result.

Example implementation (compact and robust enough for many pages):

<?php
ob_start(function ($html) {
    $blocks = [];
    $html = preg_replace_callback(
        '#<(script|style|pre|textarea)(\b[^>]*)>.*?<\/\1>#is',
        function ($m) use (&$blocks) {
            $key = '###BLOCK' . count($blocks) . '###';
            $blocks[$key] = $m[0];
            return $key;
        },
        $html
    );

    // remove HTML comments but keep conditional comments, then collapse whitespace
    $html = preg_replace('/<!--(?!\\[if).*?-->/s', '', $html);
    $html = preg_replace('/>\\s+</', '><', $html);
    $html = preg_replace('/\\s{2,}/', ' ', $html);

    return str_replace(array_keys($blocks), array_values($blocks), $html);
});
?>

Cautions and next steps: test pages with inline scripts, JSON blobs, or significant whitespace (pre/textarea). Conditional IE comments and some server-side templates need special handling. For production, consider a maintained library (for example voku/HtmlMin: https://github.com/voku/HtmlMin) rather than a homegrown regex for every edge case. Also combine minification with gzip and proper caching to get real bandwidth/latency wins.

Notes tied to thread: pointed toward an optimizer; that works for one‑off pages. Since built a custom tool, the placeholder + regex approach above highlights common pitfalls and improvements to make it safe. For regex reference see preg_replace.

Recommended Answers

All 3 Replies

You can try this online HTML optimizer,

After you've got the optimized code, you'll notice there's still some whitespace, so type this in:

<?php
$html = "HTML";
$optimized = str_replace("> <", "", $html);
echo "<code>".$optimized."</code>";
?>

I haven't tested the above PHP code, but it should work.

Is it possible to let the script send the html source to the site. The site optimizes it, and then I get it back? Is there an automated way to do is?

IT's K, I made a optimizer myself with help from annother script =p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.