For the love of white space
- ScottG
- Proficient


- Joined: Jul 06, 2010
- Posts: 260
- Status: Offline
One thing that bugs me the most is looking at code or viewing the output source of a webpage and needing to format it before I start trying to fix it. When i write code I use a lot of white space and try to comment every line so that if your were to take out the code you should be able to know what the script should do. I also make the output source code formatted so it is easier to read and debug if things are not going correctly.
So I'd thought I'd share a couple functions I wrote to help me out. Granted they are basically pointless besides the factor of formatting the source code to be read easier.
With these two functions they can turn something like
Into
Example of usage
I know I could just code the hard spaces and tabs in when doing the output but that can start to get messy like the following
With all of the examples I've displayed the script completes in 0.00025200843811035 microseconds
So I'd thought I'd share a couple functions I wrote to help me out. Granted they are basically pointless besides the factor of formatting the source code to be read easier.
PHP Code: [ Select ]
// This function is just to make code pretty and returns tabs '\t'
function tab($number = 1) {
// Make a return array
$return = array();
// Loop the number to build the \t (tabs)
for($i=0; $i < $number; $i++) {
// Add the tab to the return array
$return[] = "\t";
}
// Implode the tabs on nothing and return the tabs
return implode('', $return);
}
// This function is just to make code pretty and returns Hard spaces '\r\n', \r or \n
// can be used the default is \r\n
function hs($number = 1, $type = "\r\n") {
// Make a return array
$return = array();
// Loop the number to the hard spaces based on the type possibly passed in.
for($i=0; $i < $number; $i++) {
// Add the hard space to the return array
$return[] = $type;
}
// Implode the hard spaces on nothing and return the hard spaces
return implode('', $return);
}
function tab($number = 1) {
// Make a return array
$return = array();
// Loop the number to build the \t (tabs)
for($i=0; $i < $number; $i++) {
// Add the tab to the return array
$return[] = "\t";
}
// Implode the tabs on nothing and return the tabs
return implode('', $return);
}
// This function is just to make code pretty and returns Hard spaces '\r\n', \r or \n
// can be used the default is \r\n
function hs($number = 1, $type = "\r\n") {
// Make a return array
$return = array();
// Loop the number to the hard spaces based on the type possibly passed in.
for($i=0; $i < $number; $i++) {
// Add the hard space to the return array
$return[] = $type;
}
// Implode the hard spaces on nothing and return the hard spaces
return implode('', $return);
}
- // This function is just to make code pretty and returns tabs '\t'
- function tab($number = 1) {
- // Make a return array
- $return = array();
- // Loop the number to build the \t (tabs)
- for($i=0; $i < $number; $i++) {
- // Add the tab to the return array
- $return[] = "\t";
- }
- // Implode the tabs on nothing and return the tabs
- return implode('', $return);
- }
- // This function is just to make code pretty and returns Hard spaces '\r\n', \r or \n
- // can be used the default is \r\n
- function hs($number = 1, $type = "\r\n") {
- // Make a return array
- $return = array();
- // Loop the number to the hard spaces based on the type possibly passed in.
- for($i=0; $i < $number; $i++) {
- // Add the hard space to the return array
- $return[] = $type;
- }
- // Implode the hard spaces on nothing and return the hard spaces
- return implode('', $return);
- }
With these two functions they can turn something like
HTML Code: [ Select ]
<div><a href="#">One</a> <a href="#">Two</a> <a href="#">Three</a> <a href="#">Four</a> <a href="#">Five</a> <a href="#">Six</a> <a href="#">Seven</a> <a href="#">Eight</a> <a href="#">Nine</a> <a href="#">Ten</a></div>
- <div><a href="#">One</a> <a href="#">Two</a> <a href="#">Three</a> <a href="#">Four</a> <a href="#">Five</a> <a href="#">Six</a> <a href="#">Seven</a> <a href="#">Eight</a> <a href="#">Nine</a> <a href="#">Ten</a></div>
Into
HTML Code: [ Select ]
<div>
<a href="#">One</a>
<a href="#">Two</a>
<a href="#">Three</a>
<a href="#">Four</a>
<a href="#">Five</a>
<a href="#">Six</a>
<a href="#">Seven</a>
<a href="#">Eight</a>
<a href="#">Nine</a>
<a href="#">Ten</a>
</div>
<a href="#">One</a>
<a href="#">Two</a>
<a href="#">Three</a>
<a href="#">Four</a>
<a href="#">Five</a>
<a href="#">Six</a>
<a href="#">Seven</a>
<a href="#">Eight</a>
<a href="#">Nine</a>
<a href="#">Ten</a>
</div>
- <div>
- <a href="#">One</a>
- <a href="#">Two</a>
- <a href="#">Three</a>
- <a href="#">Four</a>
- <a href="#">Five</a>
- <a href="#">Six</a>
- <a href="#">Seven</a>
- <a href="#">Eight</a>
- <a href="#">Nine</a>
- <a href="#">Ten</a>
- </div>
Example of usage
PHP Code: [ Select ]
// This function is just to make code pretty and returns tabs '\t'
function tab($number = 1) {
// Make a return array
$return = array();
// Loop the number to build the \t (tabs)
for($i=0; $i < $number; $i++) {
// Add the tab to the return array
$return[] = "\t";
}
// Implode the tabs on nothing and return the tabs
return implode('', $return);
}
// This function is just to make code pretty and returns Hard spaces '\r\n', \r or \n
// can be used the default is \r\n
function hs($number = 1, $type = "\r\n") {
// Make a return array
$return = array();
// Loop the number to the hard spaces based on the type possibly passed in.
for($i=0; $i < $number; $i++) {
// Add the hard space to the return array
$return[] = $type;
}
// Implode the hard spaces on nothing and return the hard spaces
return implode('', $return);
}
// Make the test links
$test_array = array(
"<a href=\"#\">One</a>",
"<a href=\"#\">Two</a>",
"<a href=\"#\">Three</a>",
"<a href=\"#\">Four</a>",
"<a href=\"#\">Five</a>",
"<a href=\"#\">Six</a>",
"<a href=\"#\">Seven</a>",
"<a href=\"#\">Eight</a>",
"<a href=\"#\">Nine</a>",
"<a href=\"#\">Ten</a>"
);
// Unformatted
echo '<div>' . implode(' ', $test_array) . '</div>';
// Formatted
echo '<div>' . hs() . tab() . implode(hs() . tab(), $test_array) . hs() . '</div>';
// Other examples
echo hs() . implode(hs(), $test_array);
echo hs(2) . implode(hs(2), $test_array);
echo hs(3) . implode(hs(3), $test_array);
echo hs(2) . tab() . '<br>' . hs(2);
echo hs(1, "\r") . implode(hs(1, "\r"), $test_array);
echo hs(1, "\n") . implode(hs(1, "\n"), $test_array);
echo hs(1, "\r\n") . implode(hs(1, "\r\n"), $test_array);
echo hs(2) . tab() . '<br>' . hs(2);
echo hs() . tab() . implode(hs() . tab(), $test_array);
echo hs() . tab(2) . implode(hs() . tab(2), $test_array);
echo hs() . tab(3) . implode(hs() . tab(3), $test_array);
function tab($number = 1) {
// Make a return array
$return = array();
// Loop the number to build the \t (tabs)
for($i=0; $i < $number; $i++) {
// Add the tab to the return array
$return[] = "\t";
}
// Implode the tabs on nothing and return the tabs
return implode('', $return);
}
// This function is just to make code pretty and returns Hard spaces '\r\n', \r or \n
// can be used the default is \r\n
function hs($number = 1, $type = "\r\n") {
// Make a return array
$return = array();
// Loop the number to the hard spaces based on the type possibly passed in.
for($i=0; $i < $number; $i++) {
// Add the hard space to the return array
$return[] = $type;
}
// Implode the hard spaces on nothing and return the hard spaces
return implode('', $return);
}
// Make the test links
$test_array = array(
"<a href=\"#\">One</a>",
"<a href=\"#\">Two</a>",
"<a href=\"#\">Three</a>",
"<a href=\"#\">Four</a>",
"<a href=\"#\">Five</a>",
"<a href=\"#\">Six</a>",
"<a href=\"#\">Seven</a>",
"<a href=\"#\">Eight</a>",
"<a href=\"#\">Nine</a>",
"<a href=\"#\">Ten</a>"
);
// Unformatted
echo '<div>' . implode(' ', $test_array) . '</div>';
// Formatted
echo '<div>' . hs() . tab() . implode(hs() . tab(), $test_array) . hs() . '</div>';
// Other examples
echo hs() . implode(hs(), $test_array);
echo hs(2) . implode(hs(2), $test_array);
echo hs(3) . implode(hs(3), $test_array);
echo hs(2) . tab() . '<br>' . hs(2);
echo hs(1, "\r") . implode(hs(1, "\r"), $test_array);
echo hs(1, "\n") . implode(hs(1, "\n"), $test_array);
echo hs(1, "\r\n") . implode(hs(1, "\r\n"), $test_array);
echo hs(2) . tab() . '<br>' . hs(2);
echo hs() . tab() . implode(hs() . tab(), $test_array);
echo hs() . tab(2) . implode(hs() . tab(2), $test_array);
echo hs() . tab(3) . implode(hs() . tab(3), $test_array);
- // This function is just to make code pretty and returns tabs '\t'
- function tab($number = 1) {
- // Make a return array
- $return = array();
- // Loop the number to build the \t (tabs)
- for($i=0; $i < $number; $i++) {
- // Add the tab to the return array
- $return[] = "\t";
- }
- // Implode the tabs on nothing and return the tabs
- return implode('', $return);
- }
- // This function is just to make code pretty and returns Hard spaces '\r\n', \r or \n
- // can be used the default is \r\n
- function hs($number = 1, $type = "\r\n") {
- // Make a return array
- $return = array();
- // Loop the number to the hard spaces based on the type possibly passed in.
- for($i=0; $i < $number; $i++) {
- // Add the hard space to the return array
- $return[] = $type;
- }
- // Implode the hard spaces on nothing and return the hard spaces
- return implode('', $return);
- }
- // Make the test links
- $test_array = array(
- "<a href=\"#\">One</a>",
- "<a href=\"#\">Two</a>",
- "<a href=\"#\">Three</a>",
- "<a href=\"#\">Four</a>",
- "<a href=\"#\">Five</a>",
- "<a href=\"#\">Six</a>",
- "<a href=\"#\">Seven</a>",
- "<a href=\"#\">Eight</a>",
- "<a href=\"#\">Nine</a>",
- "<a href=\"#\">Ten</a>"
- );
- // Unformatted
- echo '<div>' . implode(' ', $test_array) . '</div>';
- // Formatted
- echo '<div>' . hs() . tab() . implode(hs() . tab(), $test_array) . hs() . '</div>';
- // Other examples
- echo hs() . implode(hs(), $test_array);
- echo hs(2) . implode(hs(2), $test_array);
- echo hs(3) . implode(hs(3), $test_array);
- echo hs(2) . tab() . '<br>' . hs(2);
- echo hs(1, "\r") . implode(hs(1, "\r"), $test_array);
- echo hs(1, "\n") . implode(hs(1, "\n"), $test_array);
- echo hs(1, "\r\n") . implode(hs(1, "\r\n"), $test_array);
- echo hs(2) . tab() . '<br>' . hs(2);
- echo hs() . tab() . implode(hs() . tab(), $test_array);
- echo hs() . tab(2) . implode(hs() . tab(2), $test_array);
- echo hs() . tab(3) . implode(hs() . tab(3), $test_array);
I know I could just code the hard spaces and tabs in when doing the output but that can start to get messy like the following
PHP Code: [ Select ]
// Make fake db info
$fake_db = array(
array("link_id" => '1', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '2', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '3', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '4', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '5', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '6', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '7', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '8', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '9', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '10', "link" => 'http://www.ozzu.com', "source" => 'Google')
);
// Make the table array
$table = array();
// Build the table header
$table[] = '<table>';
$table[] = "\t" . '<thead>';
$table[] = "\t\t" . '<tr>';
$table[] = "\t\t\t" . '<th>ID</th>';
$table[] = "\t\t\t" . '<th>Link</th>';
$table[] = "\t\t\t" . '<th>Source</th>';
$table[] = "\t\t" . '</tr>';
$table[] = "\t" . '</thead>';
$table[] = "\t" . '<tbody>';
// Loop MySql Results (fake info for demo)
foreach($fake_db as $row2) {
// Build the tables rows
$table[] = "\t\t" . '<tr>';
$table[] = "\t\t\t" . '<td>'. $row2['link_id'] .'</td>';
$table[] = "\t\t\t" . '<td>'. $row2['link'] .'</td>';
$table[] = "\t\t\t" . '<td>'. $row2['source'] .'</td>';
$table[] = "\t\t" . '</tr>';
}
// Finish the table
$table[] = "\t" . '</tbody>';
$table[] = '</table>';
// Implode the table
$table = implode("\r\n", $table) . "\r\n";
// Spit out the table
echo $table;
// Make the table array
$table = array();
// Build the table header
$table[] = '<table>';
$table[] = tab() . '<thead>';
$table[] = tab(2) . '<tr>';
$table[] = tab(3) . '<th>ID</th>';
$table[] = tab(3) . '<th>Link</th>';
$table[] = tab(3) . '<th>Source</th>';
$table[] = tab(2) . '</tr>';
$table[] = tab() . '</thead>';
$table[] = tab() . '<tbody>';
// Loop MySql Results (fake info for demo)
foreach($fake_db as $row2) {
// Build the tables rows
$table[] = tab(2) . '<tr>';
$table[] = tab(3) . '<td>'. $row2['link_id'] .'</td>';
$table[] = tab(3) . '<td>'. $row2['link'] .'</td>';
$table[] = tab(3) . '<td>'. $row2['source'] .'</td>';
$table[] = tab(2) . '</tr>';
}
// Finish the table
$table[] = tab() . '</tbody>';
$table[] = '</table>';
// Implode the table
$table = implode(hs(), $table) . hs();
// Spit out the table
echo $table;
$fake_db = array(
array("link_id" => '1', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '2', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '3', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '4', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '5', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '6', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '7', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '8', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '9', "link" => 'http://www.ozzu.com', "source" => 'Google'),
array("link_id" => '10', "link" => 'http://www.ozzu.com', "source" => 'Google')
);
// Make the table array
$table = array();
// Build the table header
$table[] = '<table>';
$table[] = "\t" . '<thead>';
$table[] = "\t\t" . '<tr>';
$table[] = "\t\t\t" . '<th>ID</th>';
$table[] = "\t\t\t" . '<th>Link</th>';
$table[] = "\t\t\t" . '<th>Source</th>';
$table[] = "\t\t" . '</tr>';
$table[] = "\t" . '</thead>';
$table[] = "\t" . '<tbody>';
// Loop MySql Results (fake info for demo)
foreach($fake_db as $row2) {
// Build the tables rows
$table[] = "\t\t" . '<tr>';
$table[] = "\t\t\t" . '<td>'. $row2['link_id'] .'</td>';
$table[] = "\t\t\t" . '<td>'. $row2['link'] .'</td>';
$table[] = "\t\t\t" . '<td>'. $row2['source'] .'</td>';
$table[] = "\t\t" . '</tr>';
}
// Finish the table
$table[] = "\t" . '</tbody>';
$table[] = '</table>';
// Implode the table
$table = implode("\r\n", $table) . "\r\n";
// Spit out the table
echo $table;
// Make the table array
$table = array();
// Build the table header
$table[] = '<table>';
$table[] = tab() . '<thead>';
$table[] = tab(2) . '<tr>';
$table[] = tab(3) . '<th>ID</th>';
$table[] = tab(3) . '<th>Link</th>';
$table[] = tab(3) . '<th>Source</th>';
$table[] = tab(2) . '</tr>';
$table[] = tab() . '</thead>';
$table[] = tab() . '<tbody>';
// Loop MySql Results (fake info for demo)
foreach($fake_db as $row2) {
// Build the tables rows
$table[] = tab(2) . '<tr>';
$table[] = tab(3) . '<td>'. $row2['link_id'] .'</td>';
$table[] = tab(3) . '<td>'. $row2['link'] .'</td>';
$table[] = tab(3) . '<td>'. $row2['source'] .'</td>';
$table[] = tab(2) . '</tr>';
}
// Finish the table
$table[] = tab() . '</tbody>';
$table[] = '</table>';
// Implode the table
$table = implode(hs(), $table) . hs();
// Spit out the table
echo $table;
- // Make fake db info
- $fake_db = array(
- array("link_id" => '1', "link" => 'http://www.ozzu.com', "source" => 'Google'),
- array("link_id" => '2', "link" => 'http://www.ozzu.com', "source" => 'Google'),
- array("link_id" => '3', "link" => 'http://www.ozzu.com', "source" => 'Google'),
- array("link_id" => '4', "link" => 'http://www.ozzu.com', "source" => 'Google'),
- array("link_id" => '5', "link" => 'http://www.ozzu.com', "source" => 'Google'),
- array("link_id" => '6', "link" => 'http://www.ozzu.com', "source" => 'Google'),
- array("link_id" => '7', "link" => 'http://www.ozzu.com', "source" => 'Google'),
- array("link_id" => '8', "link" => 'http://www.ozzu.com', "source" => 'Google'),
- array("link_id" => '9', "link" => 'http://www.ozzu.com', "source" => 'Google'),
- array("link_id" => '10', "link" => 'http://www.ozzu.com', "source" => 'Google')
- );
- // Make the table array
- $table = array();
- // Build the table header
- $table[] = '<table>';
- $table[] = "\t" . '<thead>';
- $table[] = "\t\t" . '<tr>';
- $table[] = "\t\t\t" . '<th>ID</th>';
- $table[] = "\t\t\t" . '<th>Link</th>';
- $table[] = "\t\t\t" . '<th>Source</th>';
- $table[] = "\t\t" . '</tr>';
- $table[] = "\t" . '</thead>';
- $table[] = "\t" . '<tbody>';
- // Loop MySql Results (fake info for demo)
- foreach($fake_db as $row2) {
- // Build the tables rows
- $table[] = "\t\t" . '<tr>';
- $table[] = "\t\t\t" . '<td>'. $row2['link_id'] .'</td>';
- $table[] = "\t\t\t" . '<td>'. $row2['link'] .'</td>';
- $table[] = "\t\t\t" . '<td>'. $row2['source'] .'</td>';
- $table[] = "\t\t" . '</tr>';
- }
- // Finish the table
- $table[] = "\t" . '</tbody>';
- $table[] = '</table>';
- // Implode the table
- $table = implode("\r\n", $table) . "\r\n";
- // Spit out the table
- echo $table;
- // Make the table array
- $table = array();
- // Build the table header
- $table[] = '<table>';
- $table[] = tab() . '<thead>';
- $table[] = tab(2) . '<tr>';
- $table[] = tab(3) . '<th>ID</th>';
- $table[] = tab(3) . '<th>Link</th>';
- $table[] = tab(3) . '<th>Source</th>';
- $table[] = tab(2) . '</tr>';
- $table[] = tab() . '</thead>';
- $table[] = tab() . '<tbody>';
- // Loop MySql Results (fake info for demo)
- foreach($fake_db as $row2) {
- // Build the tables rows
- $table[] = tab(2) . '<tr>';
- $table[] = tab(3) . '<td>'. $row2['link_id'] .'</td>';
- $table[] = tab(3) . '<td>'. $row2['link'] .'</td>';
- $table[] = tab(3) . '<td>'. $row2['source'] .'</td>';
- $table[] = tab(2) . '</tr>';
- }
- // Finish the table
- $table[] = tab() . '</tbody>';
- $table[] = '</table>';
- // Implode the table
- $table = implode(hs(), $table) . hs();
- // Spit out the table
- echo $table;
With all of the examples I've displayed the script completes in 0.00025200843811035 microseconds
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
January 3rd, 2013, 9:58 am
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 2 posts
- Users browsing this forum: No registered users and 99 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum

