Friday, March 2, 2012

PHP Tidy - Clean HTML Tags, HTML code, HTML Source, Comments


Website Webpage Optimization part, HTML pages contain many unnecessary development usage section such as Comments, Indents, Empty Lines, Accidental Enclosed tags without End Tag, Unknown Closing Tag, These are very sensitive for Browser Compatibly Issues, Page Loading Time and Web Page Optimization



We can able to keep our Web pages keep away from Unnecessary things such Comments, Un closed Tags, Un closed End Tags using HTML Tidy Object from PHP.

Here is the Example Code for PHP tidy Object. with Examples.
With this Example We have to use tidy Object (Tidy Object need to Enabled in PHP Configuration).
Check with phpinfo() under Tidy. ob_get_clean() , parse HTML String with parseString() function from Tidy object, and Clean our HTML with cleanRepair()




You can find full Configuration of Tidy Object at Here Click here
<?php
// Start OutputBuffer
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML Tidy Object PHP</title>
</head>
<body>

<!-- // HTML Comment by Developer and designer for code identification -->

<!-- Unclose Tag-->
<h1>Welcome to Tidy
<!-- Unclose End Tag-->
<p>This is an example for unlocsed para fix with tiday <p>


<!-- Unnecessary Blank Lines-->


<!-- Mutiple Closing Tags-->
<p>Multiple Closes</p></p>



</body>
</html>
<?php
// Store HTML Output Buffer as variable with ob_get_clean();
$html = ob_get_clean();

// Specify configuration
$config = array(
           'indent'=> false,
           'hide-comments' => true,
           'output-xhtml' => true,
           'wrap' => false
  );

// Tidy
$tidy = new tidy;
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();

// Output
echo $tidy;
?>

Output in Browser after HTML Cleanup


<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML Tidy Object PHP</title>
</head>
<body>
<h1>Welcome to Tidy </h1>
<p>This is an example for unlocsed para fix with tiday</p>
<p> </p>
<p>Multiple Closes</p>
</body>
</html>

8 comments:

  1. Hi Jailani,

    This is really helpful to me for understanding the web page loading with user perspective.

    Thanks,
    Anwar

    ReplyDelete
  2. Thanks! for this code, Nizzzzzz..

    ReplyDelete
  3. Likewise, the single quote will further enable programmers to print longer lines of information more efficiently. However, the developers have to make changes to the PHP code while using single quotes instead of double quotes.
    plakatų spausdinimas

    ReplyDelete
  4. Incredible occupation for distributing such a valuable site. Your web log isn't just helpful yet it is also extremely imaginative as well. Aerospace webdesign

    ReplyDelete
  5. I just want to let you know that I just check out your site and I find it very interesting and informative.. english language course

    ReplyDelete
  6. At the hour of starting the site, we consider different variables that can represent the moment of truth the webpage including site structuring CakePHP Application Development

    ReplyDelete
  7. I discovered your site ideal for me. It consists of wonderful and useful posts. I've read many of them and also got so much from them. In my experience, you do the truly amazing.Truly i'm impressed out of this publish Carpet Cleaning companies Fayetteville NC

    ReplyDelete
  8. However, how to go about it such that is steady, savvy, and simple to oversee? One well known arrangement is to utilize the server-side scripting language PHP to enable you to take care of those issues.Why use Laravel

    ReplyDelete