PHP: HTML-Texte „sauber“ formatieren

Nachfolgend eine sehr hilfreiche Funktion, um einen String der HTML-Code enthält als valides HTML zurückzuliefern.

Hinweis: Das Modul tidy.so muß aktiviert sein.

Danke an Quelle.

     /**
     * Liefert einen sauberen HTML-String zurück
     * @param $html
     * @param string $tidy_config
     * @return string
     */
    public function htmlTidy($html, $tidy_config = '')
    {
        $config = array(
            'show-body-only' => true,
            'clean' => true,
            'char-encoding' => 'utf8',
            'add-xml-decl' => true,
            'add-xml-space' => true,
            'output-html' => false,
            'output-xml' => false,
            'output-xhtml' => false,
            'numeric-entities' => false,
            'ascii-chars' => false,
            'doctype' => 'strict',
            'bare' => true,
            'fix-uri' => true,
            'indent' => false,
            'indent-spaces' => 0,
            'tab-size' => 0,
            'wrap-attributes' => true,
            'wrap' => 0,
            'indent-attributes' => false,
            'join-classes' => false,
            'join-styles' => false,
            'enclose-block-text' => true,
            'fix-bad-comments' => true,
            'fix-backslash' => true,
            'replace-color' => false,
            'wrap-asp' => false,
            'wrap-jste' => false,
            'wrap-php' => false,
            'write-back' => true,
            'drop-proprietary-attributes' => false,
            'hide-comments' => true,
            'hide-endtags' => false,
            'literal-attributes' => false,
            'drop-empty-paras' => true,
            'drop-font-tags' => true,
            'enclose-text' => true,
            'quote-ampersand' => true,
            'quote-marks' => false,
            'quote-nbsp' => true,
            'vertical-space' => true,
            'wrap-script-literals' => false,
            'tidy-mark' => true,
            'merge-divs' => false,
            'repeated-attributes' => 'keep-last',
            'break-before-br' => false,
        );

        if ($tidy_config == '') {
            $tidy_config = & $config;
        }

        $tidy = new tidy();
        $tidyString = $tidy->repairString($html, $tidy_config, 'UTF8');
        $out = $tidyString;
        unset($tidy);
        unset($tidy_config);
        return ($out);
    }
Dieser Beitrag wurde unter Magento, PHP veröffentlicht. Setze ein Lesezeichen auf den Permalink.