{"id":55256,"date":"2022-03-29T03:01:02","date_gmt":"2022-03-28T17:01:02","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=55256"},"modified":"2022-03-28T18:51:56","modified_gmt":"2022-03-28T08:51:56","slug":"html-online-validation-via-php-domdocument-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/html-online-validation-via-php-domdocument-tutorial\/","title":{"rendered":"HTML Online Validation via PHP DOMDocument Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"HTML Online Validation via PHP DOMDocument Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example_dd.jpg\" title=\"HTML Online Validation via PHP DOMDocument Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">HTML Online Validation via PHP DOMDocument Tutorial<\/p><\/div>\n<p>The lack of platform functionality that yesterday&#8217;s <a title='HTML Online Validation via PHP Tidy Primer Tutorial' href='#htmlovphptpt'>HTML Online Validation via PHP Tidy Primer Tutorial<\/a> &#8220;Intranet feeling&#8221; <i>Online HTML Validator<\/i> web application was supported by, got us interested in exploring HTML validation via PHP <a target=_blank title=DOMDocument href='https:\/\/www.php.net\/manual\/en\/book.dom.php'>DOMDocument<\/a> methodologies.<\/p>\n<p>Yes, (serverside) PHP can work with (its version of) DOM (document object model) but not as well as (clientside) Javascript DOM can.  The validation of HTML at the serverside (PHP) stage does not take into account web page rendering, and so struggles to detect &#8220;well formed looking&#8221; HTML (but) with HTML tags that mean nothing.  But we can do some validation checking via &#8230;<\/p>\n<p>&lt;?php<br \/>\n<code><br \/>\nfunction nodeContent($n, $outer=false) { \/\/ thanks to <a target=_blank title='https:\/\/stackoverflow.com\/questions\/5404941\/how-to-return-outer-html-of-domdocument' href='https:\/\/stackoverflow.com\/questions\/5404941\/how-to-return-outer-html-of-domdocument'>https:\/\/stackoverflow.com\/questions\/5404941\/how-to-return-outer-html-of-domdocument<\/a><br \/>\n    global $ioff, $origh;<br \/>\n    $d = new DOMDocument();<br \/>\n    $b = $d-&gt;importNode($n-&gt;cloneNode(true),true);<br \/>\n    $d-&gt;appendChild($b);<br \/>\n    $h = $d-&gt;saveHTML();<br \/>\n    \/\/ remove outter tags<br \/>\n    if (!$outer) {<br \/>\n     $h = substr($h,strpos($h,'&gt;')+1,-(strlen($n-&gt;nodeName)+4));<br \/>\n    } else {<br \/>\n     if ($ioff == -2 && strpos(strtolower($h), '&lt;html') !== false) {  return '';   }<br \/>\n     if ($ioff &lt;= -1 && strpos(strtolower($h), '&lt;body') !== false) {  return '';   }<br \/>\n    }<br \/>\n    if (stripos( str_replace(' ','',str_replace('\"','',$origh)),str_replace(' ','',str_replace(\"\\n\",'',str_replace('\"','',('' . $h))))  ) !== false) {<br \/>\n      return '&amp;#10004; ' . $h;<br \/>\n    } else if (1 == 1) {<br \/>\n      return '&amp;#10060; ' . $h;<br \/>\n    }<br \/>\n    return '' . $h;<br \/>\n}<br \/>\n<br \/>\n  $doc = new DOMDocument();<br \/>\n  $doc-&gt;loadHTML($htmlin);<br \/>\n  foreach ($doc-&gt;getElementsByTagName(\"*\") as $item) {<br \/>\n    $elecnt++;<br \/>\n    if ($rest == \"\") { $rest = \" ... \"; }<br \/>\n    $rest.=' &lt;br&gt;' . str_replace('&gt;','&gt;',str_replace('&lt;','&lt;',nodeContent($item, true)));<br \/>\n  }<br \/>\n  $twc='Number of elements detected is ' . ($ioff + $elecnt) . $rest;<br \/>\n<\/code><br \/>\n?&gt;<\/p>\n<p><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example.php-GETME\" title=\"tidy_example.php\">The changed<\/a> <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example.php-GETME\" title=\"tidy_example.php\">tidy_example.php<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example.php\" title=\"Click picture\">proof of concept PHP online HTML validator web application<\/a> you can try <a href='#myifdd' title='Iframe usage'>here also<\/a>, the mobile platform versions of which only allow the DOMDocument methodology be used.<\/p>\n<p><!--p>You can also see this play out at WordPress 4.1.1's <a target=_blank  href='\/\/www.rjmprogramming.com.au\/ITblog\/html-online-validation-via-php-docdocument-tutorial\/'>HTML Online Validation via PHP DOMDocument Tutorial<\/a>.<\/p-->\n<hr>\n<p id='htmlovphptpt'>Previous relevant <a target=_blank title='HTML Online Validation via PHP Tidy Primer Tutorial' href='\/\/www.rjmprogramming.com.au\/ITblog\/html-online-validation-via-php-tidy-primer-tutorial\/'>HTML Online Validation via PHP Tidy Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"HTML Online Validation via PHP Tidy Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example.jpg\" title=\"HTML Online Validation via PHP Tidy Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">HTML Online Validation via PHP Tidy Primer Tutorial<\/p><\/div>\n<p>In terms of your, perhaps <a target=_blank title='MAMP local Apache\/PHP\/MySql web server' href='http:\/\/mamp.info'>MAMP<\/a>, Apache\/PHP\/MySql local web server&#8217;s &#8220;low hanging fruit&#8221; for extending PHP functionality, what are the most straightforward PHP modules to look at?  We&#8217;d say &#8230;<\/p>\n<ol>\n<li>find your Apache\/PHP\/MySql local web server&#8217;s php.ini (or equivalent) &#8230;<\/li>\n<li>(text) edit this php.ini &#8230;<\/li>\n<li>find &#8220;extension=&#8221; section and see those that are commented out &#8230;<\/li>\n<li>these PHP modules will have been installed but held back from being activated &#8230;<\/li>\n<li>you can then uncomment these &#8230; <\/li>\n<li>File -&gt; Save the new version of php.ini &#8230; then &#8230;<\/li>\n<li>stop and start your Apache\/PHP\/MySql local web server<\/li>\n<\/ol>\n<p> &#8230; and we found on our macOS Apache\/PHP\/MySql local web server the chance to introduce into our thinking, the use of the PHP module called <a target=_blank title='PHP tidy module' href='https:\/\/www.php.net\/manual\/en\/book.tidy.php'>&#8220;tidy&#8221;<\/a>.  Doing the research and development on this lead us to believe we could develop an &#8220;online HTML validator&#8221; (for small amounts of HTML on the RJM Programming domain version, initially), with <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example.php_GETME\" title=\"tidy_example.php\">tidy_example.php<\/a> (which we&#8217;d want you to download to the <a target=_blank title='Apache document root' href='https:\/\/www.google.com\/search?q=document+root+apache&#038;rlz=1C5CHFA_enAU973AU973&#038;oq=document+root+apache&#038;aqs=chrome..69i57j0i512l3j0i22i30l6.4801j0j4&#038;sourceid=chrome&#038;ie=UTF-8'>document root<\/a> folder of your, perhaps MAMP, Apache\/PHP\/MySql local web server, to enable our &#8220;Intranet&#8221; feeling modus operandi up at the RJM Programming public domain) <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example.php\" title=\"Click picture\">proof of concept PHP online HTML validator web application<\/a> &#8230;<\/p>\n<p><iframe id=myifdd src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/tidy_example.php\" style=\"width:100%;height:1600px;\"><\/iframe><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d55244' onclick='var dv=document.getElementById(\"d55244\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/module\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d55244' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d55256' onclick='var dv=document.getElementById(\"d55256\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/dom\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d55256' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The lack of platform functionality that yesterday&#8217;s HTML Online Validation via PHP Tidy Primer Tutorial &#8220;Intranet feeling&#8221; Online HTML Validator web application was supported by, got us interested in exploring HTML validation via PHP DOMDocument methodologies. Yes, (serverside) PHP can &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/html-online-validation-via-php-domdocument-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[85,1750,354,356,360,576,744,802,932,997,1262,3945,1319,1357,1358],"class_list":["post-55256","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-apache","tag-document-root","tag-dom","tag-domdocument","tag-download","tag-html","tag-mamp","tag-module","tag-php","tag-programming","tag-textarea","tag-tidy","tag-tutorial","tag-validate","tag-validation"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/55256"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=55256"}],"version-history":[{"count":5,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/55256\/revisions"}],"predecessor-version":[{"id":55261,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/55256\/revisions\/55261"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=55256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=55256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=55256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}