Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:50:45+00:00 2026-06-08T20:50:45+00:00

I’m using php extension tidy-html to clean up php output. I know that tidy

  • 0

I’m using php extension tidy-html to clean up php output. I know that tidy removes invalid tags and can’t even handle HTML5 doctype, but I’m using tag <menu> which used to be in HTML specifications. However, it gets changed for <ul> anyway.

Oddly enough, It didn’t do so before. I changed the tidy config and it has break. Now I’ve turned off all options that messes with tags, but it didn’t help.

My script is quite verbose:

$tidy_config = array(
    'char-encoding' => 'utf8',
    'output-encoding' => 'utf8',
    'output-html' => true,
    'numeric-entities' => false,
    'ascii-chars' => false,
    'doctype' => 'loose',
    'clean' => false,
    'bare' => false,
    'fix-uri' => true,
    'indent' => true,
    'indent-spaces' => 2,
    'tab-size' => 2,
    'wrap-attributes' => true,
    'wrap' => 0,
    'indent-attributes' => true,
    'join-classes' => false,
    'join-styles' => false,
    'fix-bad-comments' => true,
    'fix-backslash' => true,
    'replace-color' => false,
    'wrap-asp' => false,
    'wrap-jste' => false,
    'wrap-php' => false,
    'wrap-sections' => false,
    'drop-proprietary-attributes' => false,
    'hide-comments' => false,
    'hide-endtags' => false,
    'drop-empty-paras' => true,
    'quote-ampersand' => true,
    'quote-marks' => true,
    'quote-nbsp' => true,
    'vertical-space' => true,
    'wrap-script-literals' => false,
    'tidy-mark' => true,
    'merge-divs' => false,
    'repeated-attributes' => 'keep-last',
    'break-before-br' => false
);

$tidy_config2 = array(
    'tidy-mark' => false,
    'vertical-space' => false,
    'hide-comments' => true,
    'indent-spaces' => 0,
    'tab-size' => 1,
    'wrap-attributes' => false,
    'numeric-entities' => true,
    'ascii-chars' => true,
    'hide-endtags' => true,
    'indent' => false
);
$tidy_config = array_merge($tidy_config, $tidy_config2);

$dtm = preg_match(self::doctypeMatch, $output, $dt);
$output = tidy_repair_string($output, $tidy_config, 'utf8');

// tidy screws up doctype --fixed
if($dtm)
    $output = preg_replace(self::doctypeMatch, $dt[0], $output);

$output = preg_replace('!>[\n\r]+<!', '><', $output);

unset($tidy_config);

return $output;

Note that it is more complicated than this (hence the two arrays). I’ve just cut off unnecessary code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:50:46+00:00Added an answer on June 8, 2026 at 8:50 pm

    DISCLAIMER:

    I don’t think my answer is very… neat. It’s more of a hakish way to use HTMLTidy with HTML5 (which currently it does not support). To accomplish that I use regex to parse HTML, which, according to most, is the the root of all evil or the cthulhu way. If someone knows a better way, please enlighten us, since I don’t feel very secure in using regex to parse html. I’ve tested it with many examples but I’m quite sure it’s not bullet proof.

    Intro

    The menu tag was deprecated in HTML4 and XHTML1, being replaced by ul (unordered list). It was, however, redefined in HTML5 and hence is a valid tag according to HTML5 specifications. SinceHTMLTidy does not support HTML5 and uses XHTML or HTML specifications, as the OP pointed, it replaces the then deprecated tag menu to ul (or adds the ul tag), even when you specifically tell it not to.

    My suggestion

    This function replaces the menu tag with a custom tag prior to parsing it with tidy. It then replaces the custom tag with menu again.

    function tidyHTML5($buffer)
    {
        $buffer = str_replace('<menu', '<mytag', $buffer);
        $buffer = str_replace('menu>', 'mytag>', $buffer);
        $tidy = new tidy();
        $options = array(
                'hide-comments'         => true,
                'tidy-mark'             => false,
                'indent'                => true,
                'indent-spaces'         => 4,
                'new-blocklevel-tags'   => 'menu,mytag,article,header,footer,section,nav',
                'new-inline-tags'       => 'video,audio,canvas,ruby,rt,rp',
                'doctype'               => '<!DOCTYPE HTML>',
                //'sort-attributes'     => 'alpha',
                'vertical-space'        => false,
                'output-xhtml'          => true,
                'wrap'                  => 180,
                'wrap-attributes'       => false,
                'break-before-br'       => false,
                'char-encoding'         => 'utf8',
                'input-encoding'        => 'utf8',
                'output-encoding'       => 'utf8'
        );
    
        $tidy->parseString($buffer, $options, 'utf8');
        $tidy->cleanRepair();
    
        $html = '<!DOCTYPE HTML>' . PHP_EOL . $tidy->html();
        $html = str_replace('<html lang="en" xmlns="http://www.w3.org/1999/xhtml">', '<html>', $html);
        $html = str_replace('<html xmlns="http://www.w3.org/1999/xhtml">', '<html>', $html);
    
        //Hackish stuff starts here
        //We use regex to parse html, which is usually a bad idea
        //But currently there is no alternative to it, since tidy is not MENU TAG friendly
        preg_match_all('/\<mytag(?:[^\>]*)\>\s*\<ul>/', $html, $matches);
        foreach($matches as $m) {
            $mo = $m;
            $m = str_replace('mytag', 'menu', $m);
            $m = str_replace('<ul>', '', $m);
            $html = str_replace($mo, $m, $html);
        }
        $html = str_replace('<mytag', '<menu', $html);
        $html = str_replace('</ul></mytag>', '</menu>', $html);
        $html = str_replace('mytag>', 'menu>', $html);
        return $html;
    }
    

    TEST:

    header("Content-type: text/plain");
    echo tidyHTML5('<menu><li>Lorem ipsum</li></menu><div></div><menu   ><a href="#">lala</a><form id="jj"><button>btn</button></form></menu><menu style="color: white" id="nhecos"><li>blabla</li><li>sdfsdfsdf</li></menu>');
    

    OUTPUT:

    <!DOCTYPE HTML>
    <html>
        <head>
            <title></title>
        </head>
        <body>
            <menu>
    
                <li>Lorem ipsum
                </li>
            </menu><menu style="color: white" id="nhecos">
    
                <li>blabla
                </li>
                <li>sdfsdfsdf
                </li>
            </menu>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.