<?php
// tidy_example.php
// Using tiday
// RJM Programming - March, 2022 - Thanks to https://www.php.net/manual/en/function.tidy-warning-count.php

$trstyle="";
$origh="";
$ioff=0;
$incv="tidy_parse_string";
$rest="";
$twc="";
$tec="";
$elecnt=0;
$there=true;
$htmlin = '<p>first</p>
<p>test</i>
<bogustag>bogus</bogustag>';

if (isset($_GET['yourhtml'])) {
  $incv="tidy_parse_string";
  $htmlin=str_replace('+',' ',urldecode($_GET['yourhtml']));
  if (strpos(strtolower($htmlin), '<html') === false) { $ioff--; }
  if (strpos(strtolower($htmlin), '<body') === false) { $ioff--; }
} else if (isset($_POST['yourhtml'])) { 
  $incv="tidy_parse_string";
  $htmlin=str_replace('+',' ',urldecode($_POST['yourhtml']));
  if (strpos(strtolower($htmlin), '<html') === false) { $ioff--; }
  if (strpos(strtolower($htmlin), '<body') === false) { $ioff--; }
}

//file_put_contents('xxx.xxx', $htmlin);
$origh=$htmlin;

function outerHTML($e) {
     global $ioff, $origh;
     $docx = new DOMDocument();
     $docx->appendChild($docx->importNode($e, true));
     $dfx=$docx->saveHTML();
     if ($ioff == -2 && strpos(strtolower($dfx), '<html') !== false) {  return '';   }
     if ($ioff <= -1 && strpos(strtolower($dfx), '<body') !== false) {  return '';   }
     return $dfx;
}

function nodeContent($n, $outer=false) { // thanks to https://stackoverflow.com/questions/5404941/how-to-return-outer-html-of-domdocument
    global $ioff, $origh;
    $d = new DOMDocument();
    $b = $d->importNode($n->cloneNode(true),true);
    $d->appendChild($b); 
    $h = $d->saveHTML();
    // remove outter tags
    if (!$outer) {
     $h = substr($h,strpos($h,'>')+1,-(strlen($n->nodeName)+4));
    } else {
     if ($ioff == -2 && strpos(strtolower($h), '<html') !== false) {  return '';   }
     if ($ioff <= -1 && strpos(strtolower($h), '<body') !== false) {  return '';   }
    }
    if (stripos( str_replace('&nbsp;','',str_replace(' ','',str_replace('"','',$origh))),str_replace('&nbsp;','',str_replace(' ','',str_replace("\n",'',str_replace('"','',('' . $h)))))  ) !== false) {
      //file_put_contents('xx.xx','' . $h);
      return '&#10004; ' . $h;
    } else if (1 == 1) {
      //file_put_contents('x.x','' . $h . "\n" . str_replace('"','',$origh));
      return '&#10060; ' . $h;
    }
    return '' . $h;
}


if (function_exists($incv)) {
  try {
    $tidy = tidy_parse_string($htmlin);
    $twc='' . tidy_warning_count($tidy) . ' warnings';
    $tec='' . tidy_error_count($tidy) . ' errors';
  } catch (Exception $ex) {
    $there=false;
    $tec='No tidy module detected.';
  } 
} else {
  $tec='No tidy module detected.';
  $there=false;
  $doc = new DOMDocument();
  $doc->loadHTML($htmlin);
  //foreach ($doc->childNodes as $itemcn) {
    //if ($rest == "") { $rest = " +++ "; }
    //$rest.=' ' . str_replace('>','&gt;',str_replace('<','&lt;',nodeContent($itemcn, false)));
  //}
  foreach ($doc->getElementsByTagName("*") as $item) {
    $elecnt++;
    if ($rest == "") { $rest = " ... "; }
    //$rest.=' <br>' . str_replace('>','&gt;',str_replace('<','&lt;',outerHTML($item)));
    $rest.=' <br>' . str_replace('>','&gt;',str_replace('<','&lt;',nodeContent($item, true)));
  }
  $twc='Number of elements detected is ' . ($ioff + $elecnt) . $rest;
}

//echo tidy_error_count($tidy) . "\n"; //1
//echo tidy_warning_count($tidy) . "\n"; //5
if (!$there && strpos($_SERVER['SERVER_NAME'], 'localhost') === false) {
echo "<html><head><title>Using tidy</title></head><body onload=\" if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {  for (var ii=1; ii<=4; ii++) { document.getElementById('tr' + ii).style.display='none';  } } \"><h1>Using tidy</h1><h2>RJM Programming - March, 2022 - Thanks to <a target=_blank href=https://www.php.net/manual/en/function.tidy-warning-count.php>https://www.php.net/manual/en/function.tidy-warning-count.php</a><h3>" . $tec . "</h3><h4>" . $twc . "</h4><table style=width:95%;><tr id=tr1" . $trstyle . "><th>Mac OS X or macOS MAMP</th></tr><tr id=tr2" . $trstyle . "><td><form target=_self method=GET action=HTTP://localhost:8888/tidy_example.php><textarea style=width:90%;height:200px;background-color:#f0f0f0; id=yourhtml name=yourhtml>" . $htmlin . "</textarea><br><br><input style=background-color:yellow;;color:black; type=submit value=Validate></input></form></td></tr><tr id=tr3" . $trstyle . "><th>Windows MAMP</th></tr><tr id=tr4" . $trstyle . "><td><form target=_self method=GET action=HTTP://localhost/tidy_example.php><textarea style=width:90%;height:200px;background-color:#f0f0f0; id=yourhtml name=yourhtml>" . $htmlin . "</textarea><br><br><input style=background-color:yellow;color:black; type=submit value=Validate></input></form></td><tr><th>PHP DomDocument</th></tr><tr><td><form id=myformp method=POST action=./tidy_example.php><textarea style=width:90%;height:300px;background-color:#f0f0f0; id=yourhtml name=yourhtml>" . $htmlin . "</textarea><br><br><input style=background-color:yellow;color:black; type=submit value=Validate></input></form></td></tr></table></body></html>";
} else if (!$isthere) {
echo "<html><head><title>Using tidy</title></head><body><h1>Using tidy</h1><h2>RJM Programming - March, 2022 - Thanks to <a target=_blank href=https://www.php.net/manual/en/function.tidy-warning-count.php>https://www.php.net/manual/en/function.tidy-warning-count.php</a><h3>" . $tec . "</h3><h4>" . $twc . "</h4><form id=myform method=POST action=./tidy_example.php><textarea style=width:90%;height:300px;background-color:#f0f0f0; id=yourhtml name=yourhtml>" . $htmlin . "</textarea><br><br><input style=background-color:yellow;;color:black; type=submit value=Validate></input></form></body></html>";
} else {
echo "<html><head><title>Using tidy</title></head><body><h1>Using tidy</h1><h2>RJM Programming - March, 2022 - Thanks to <a target=_blank href=https://www.php.net/manual/en/function.tidy-warning-count.php>https://www.php.net/manual/en/function.tidy-warning-count.php</a><h3>" . $twc . "</h3><h4>" . $twc . "</h4><form id=myform method=POST action=./tidy_example.php><textarea style=width:90%;height:300px;background-color:#f0f0f0; id=yourhtml name=yourhtml>" . $htmlin . "</textarea><br><br><input style=background-color:yellow;;color:black; type=submit value=Validate></input></form></body></html>";
}
?>