PHP - Tidy extension

From LXF Wiki

(Difference between revisions)
Revision as of 12:56, 13 Apr 2009
Doru001 (Talk | contribs)
Spring cleaning
← Go to previous diff
Revision as of 02:35, 18 Nov 2010
Egecakibi (Talk | contribs)

Go to next diff →
Line 1: Line 1:
-= Practical PHP =+>= Practical PHP =
''(Original version written by Paul Hudson for LXF issue 64.)'' ''(Original version written by Paul Hudson for LXF issue 64.)''
Line 11: Line 11:
Another part of the problem is that few people see any need for Tidy at all: it smartens up the HTML output of your PHP scripts - it pretty-prints it. Consider this piece of PHP code: Another part of the problem is that few people see any need for Tidy at all: it smartens up the HTML output of your PHP scripts - it pretty-prints it. Consider this piece of PHP code:
-<pre>+&lt;pre&gt;
-<?php +&lt;?php
$monkeys = array('Minky','Manky','Stinky','Woopsy','Fuzzy','Scuzzy','Bubbles'); // define monkey array $monkeys = array('Minky','Manky','Stinky','Woopsy','Fuzzy','Scuzzy','Bubbles'); // define monkey array
$num_monkeys = count($monkeys); // count names in array $num_monkeys = count($monkeys); // count names in array
-for ($i = 0; $i < $num_monkeys; $i++) echo $monkeys[$i]. "<br />"; // loop names+for ($i = 0; $i &lt; $num_monkeys; $i++) echo $monkeys[$i]. &quot;&lt;br /&gt;&quot;; // loop names
-?>+?&gt;
-</pre>+&lt;/pre&gt;
-This functioning code fragment loops through an array holding the names of monkeys, printing out the names separated by HTML line breaks. While this might display just fine on-screen, anyone who clicks "View Source" in their browser will see a mess:+This functioning code fragment loops through an array holding the names of monkeys, printing out the names separated by HTML line breaks. While this might display just fine on-screen, anyone who clicks &quot;View Source&quot; in their browser will see a mess:
-<pre>+&lt;pre&gt;
-Minky<br />Manky<br />Stinky<br />Woopsy<br />Fuzzy<br />Scuzzy<br />Bubbles+Minky&lt;br /&gt;Manky&lt;br /&gt;Stinky&lt;br /&gt;Woopsy&lt;br /&gt;Fuzzy&lt;br /&gt;Scuzzy&lt;br /&gt;Bubbles
-</pre>+&lt;/pre&gt;
By comparison, the use of tidy on the above code gives the following result: By comparison, the use of tidy on the above code gives the following result:
-<pre>+&lt;pre&gt;
- Minky<br />+ Minky&lt;br /&gt;
- Manky<br />+ Manky&lt;br /&gt;
- Stinky<br />+ Stinky&lt;br /&gt;
- Woopsy<br />+ Woopsy&lt;br /&gt;
- Fuzzy<br />+ Fuzzy&lt;br /&gt;
- Scuzzy<br />+ Scuzzy&lt;br /&gt;
- Bubbles<br />+ Bubbles&lt;br /&gt;
-</pre>+&lt;/pre&gt;
-So, the names are separated by HTML line breaks that look fine sent through a web browser, but they don't actually have any textual lines break to make source code reading easier. "Aha!" you say, "I didn't want those dirty pirates stealing my code anyway!" Perhaps. However, this is an Open Source world: I've learnt a huge amount about HTML (probably more than is safe for any one person to know) by examining the code of others, and I think it's important to help others learn, too. Sure, they /could/ decipher our HTML if they were sufficiently talented, but a little help from Tidy will make their lives much easier, while makes us look like benevolent code geniuses. Everyone's a winner!+So, the names are separated by HTML line breaks that look fine sent through a web browser, but they don't actually have any textual lines break to make source code reading easier. &quot;Aha!&quot; you say, &quot;I didn't want those dirty pirates stealing my code anyway!&quot; Perhaps. However, this is an Open Source world: I've learnt a huge amount about HTML (probably more than is safe for any one person to know) by examining the code of others, and I think it's important to help others learn, too. Sure, they /could/ decipher our HTML if they were sufficiently talented, but a little help from Tidy will make their lives much easier, while makes us look like benevolent code geniuses. Everyone's a winner!
 + 
 +----
 +<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +----
 +=[http://uwireli.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
 +----
 +=[http://uwireli.co.cc CLICK HERE]=
 +----
 +</div>

Revision as of 02:35, 18 Nov 2010

>= Practical PHP =

(Original version written by Paul Hudson for LXF issue 64.)


Tidying up after yourself is easier than you thought...


The Tidy PHP extension is one of the least understood out there, despite its name giving away what it does. Part of the problem is that it was being written alongside development of PHP 5, and so saw substantial revision before it was finalised - if you used an early version of Tidy, you can be guaranteed you need to relearn it now.

Another part of the problem is that few people see any need for Tidy at all: it smartens up the HTML output of your PHP scripts - it pretty-prints it. Consider this piece of PHP code:

<pre> <?php $monkeys = array('Minky','Manky','Stinky','Woopsy','Fuzzy','Scuzzy','Bubbles'); // define monkey array $num_monkeys = count($monkeys); // count names in array for ($i = 0; $i < $num_monkeys; $i++) echo $monkeys[$i]. "<br />"; // loop names ?> </pre>

This functioning code fragment loops through an array holding the names of monkeys, printing out the names separated by HTML line breaks. While this might display just fine on-screen, anyone who clicks "View Source" in their browser will see a mess:

<pre> Minky<br />Manky<br />Stinky<br />Woopsy<br />Fuzzy<br />Scuzzy<br />Bubbles </pre>

By comparison, the use of tidy on the above code gives the following result:

<pre>

Minky<br />
Manky<br />
Stinky<br />
Woopsy<br />
Fuzzy<br />
Scuzzy<br />
Bubbles<br />

</pre>

So, the names are separated by HTML line breaks that look fine sent through a web browser, but they don't actually have any textual lines break to make source code reading easier. "Aha!" you say, "I didn't want those dirty pirates stealing my code anyway!" Perhaps. However, this is an Open Source world: I've learnt a huge amount about HTML (probably more than is safe for any one person to know) by examining the code of others, and I think it's important to help others learn, too. Sure, they /could/ decipher our HTML if they were sufficiently talented, but a little help from Tidy will make their lives much easier, while makes us look like benevolent code geniuses. Everyone's a winner!



This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page (http://uwireli.co.cc)


CLICK HERE (http://uwireli.co.cc)