en

designoir

TrimHTML

TrimHTML entfernt unnötige Whitespaces aus (X)HTML-Quelltext, was sinnvoll ist um Traffic zu sparen. Es wird schwer lesbarer Code erzeugt, womit die Klasse das genaue Gegenteil von IndentHTML ist.

TrimHTML.php

Benötigt: Preprocess

<?php

/**
 * class TrimHTML
 * Copyright (C) 2006  Dao Gottwald  <dao at design-noir.de>
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 *   http://www.opensource.org/licenses/lgpl-license.php
 *
 * @version  1.0.1
 */

require_once 'Preprocess.php';

class TrimHTML extends Preprocess {
    public function run ($text) {
        $this->text =& $text;
        $this->_escape_init_plain();
        $this->_escape_comments_cdata();
        $this->_escape_content ('pre', 'textarea');
        $this->_escape_inline_tags();
        $text = preg_replace ('~\s*(</?option(?: .*?|/)?>)\s*~is', '\\1', $text);
        $this->_escape_tags ('select');
        $text = preg_replace ('~\s+~', ' ', $text);
        $text = str_replace (' <', '<', $text);
        $text = str_replace ('> ', '>', $text);
        $this->_escape_fin();
        return $text;
    }
}

?>

Beispiel #1: trim.php

<?php

ob_start ('htmlspecialchars');
ob_start (array (new TrimHTML, 'run'));

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
    <head>
        <title>Lorem Ipsum</title>
    </head>
    <body>
        <h1>
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit
        </h1>
        <p>
            Etiam faucibus ligula ut arcu. Maecenas sed ipsum. Fusce sit amet leo at felis placerat convallis. <br/>
            Aenean vulputate nibh ut justo. <a href="#">Pellentesque</a>.
        </p>
        <p>
            Quisque viverra dui a enim molestie blandit.
            <strong>Suspendisse</strong> orci metus, volutpat sit amet, lobortis id, cursus in, mi.
            Cras faucibus nisl sit amet diam. Maecenas imperdiet aliquet lacus. Suspendisse neque.
        </p>
        
        <pre>
        .                            .                      .
.                  .             -)------+====+       .
                         -)----====    ,'   ,'   .                 .
            .                  `.  `.,;___,'                .
                                 `, |____l_\
                   _,....------c==]""______ |,,,,,,.....____ _
  .      .        "-:_____________  |____l_|]'''''''''''       .     .
                                ,'"",'.   `.
       .                 -)-----====   `.   `.              LS
                   .            -)-------+====+       .            .
           .                               .
Incom's T-65B X-wing Space
Superiority Starfighter (1)</pre>
        
        <table>
            <caption>
                Lorem ipsum dolor sit amet, consectetuer adipiscing elit
            </caption>
            <thead>
                <tr>
                    <th>
                        foo
                    </th>
                    <th>
                        bar
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        Cras posuere aliquam diam.
                        Donec venenatis, diam ac vestibulum gravida.
                    </td>
                    <td>
                        Vivamus aliquet.
                        Pellentesque habitant morbi tristique senectus et netus et.
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

trim.php ausführen

Kommentare

HTML wird nicht interpretiert.

Geändert am 30. Mai ’07 Dão G., 2005–2010
aggressiv akt andromeda bar beine blue efeu frontal fugaetu industriell komet land noir rost rot sonnenblume splash split winter wolke zeit