For the love of white space

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

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.

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);
   
}
 
  1. // This function is just to make code pretty and returns tabs '\t'
  2. function tab($number = 1) {
  3.    
  4.    // Make a return array
  5.    $return = array();
  6.    
  7.    // Loop the number to build the \t (tabs)
  8.    for($i=0; $i < $number; $i++) {
  9.      
  10.       // Add the tab to the return array
  11.       $return[] = "\t";
  12.      
  13.    }
  14.    
  15.    // Implode the tabs on nothing and return the tabs
  16.    return implode('', $return);
  17.    
  18. }
  19.  
  20. // This function is just to make code pretty and returns Hard spaces '\r\n', \r or \n
  21. // can be used the default is \r\n
  22. function hs($number = 1, $type = "\r\n") {
  23.    
  24.    // Make a return array
  25.    $return = array();
  26.    
  27.    // Loop the number to the hard spaces based on the type possibly passed in.
  28.    for($i=0; $i < $number; $i++) {
  29.      
  30.       // Add the hard space to the return array
  31.       $return[] = $type;
  32.      
  33.    }
  34.    
  35.    // Implode the  hard spaces on nothing and return the hard spaces
  36.    return implode('', $return);
  37.    
  38. }
  39.  


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>
 
  1. <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>
  2.  


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>
 
  1. <div>
  2.    <a href="#">One</a>
  3.    <a href="#">Two</a>
  4.    <a href="#">Three</a>
  5.    <a href="#">Four</a>
  6.    <a href="#">Five</a>
  7.    <a href="#">Six</a>
  8.    <a href="#">Seven</a>
  9.    <a href="#">Eight</a>
  10.    <a href="#">Nine</a>
  11.    <a href="#">Ten</a>
  12. </div>
  13.  


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);
 
  1. // This function is just to make code pretty and returns tabs '\t'
  2. function tab($number = 1) {
  3.    
  4.    // Make a return array
  5.    $return = array();
  6.    
  7.    // Loop the number to build the \t (tabs)
  8.    for($i=0; $i < $number; $i++) {
  9.      
  10.       // Add the tab to the return array
  11.       $return[] = "\t";
  12.      
  13.    }
  14.    
  15.    // Implode the tabs on nothing and return the tabs
  16.    return implode('', $return);
  17.    
  18. }
  19.  
  20. // This function is just to make code pretty and returns Hard spaces '\r\n', \r or \n
  21. // can be used the default is \r\n
  22. function hs($number = 1, $type = "\r\n") {
  23.    
  24.    // Make a return array
  25.    $return = array();
  26.    
  27.    // Loop the number to the hard spaces based on the type possibly passed in.
  28.    for($i=0; $i < $number; $i++) {
  29.      
  30.       // Add the hard space to the return array
  31.       $return[] = $type;
  32.      
  33.    }
  34.    
  35.    // Implode the  hard spaces on nothing and return the hard spaces
  36.    return implode('', $return);
  37.    
  38. }
  39.  
  40. // Make the test links
  41. $test_array = array(
  42.          "<a href=\"#\">One</a>",
  43.          "<a href=\"#\">Two</a>",
  44.          "<a href=\"#\">Three</a>",
  45.          "<a href=\"#\">Four</a>",
  46.          "<a href=\"#\">Five</a>",
  47.          "<a href=\"#\">Six</a>",
  48.          "<a href=\"#\">Seven</a>",
  49.          "<a href=\"#\">Eight</a>",
  50.          "<a href=\"#\">Nine</a>",
  51.          "<a href=\"#\">Ten</a>"
  52.          );
  53.  
  54. // Unformatted
  55. echo '<div>' . implode(' ', $test_array) . '</div>';
  56.  
  57. // Formatted
  58. echo '<div>' . hs() . tab() . implode(hs() . tab(), $test_array) . hs() . '</div>';
  59.  
  60. // Other examples
  61. echo hs() . implode(hs(), $test_array);
  62. echo hs(2) . implode(hs(2), $test_array);
  63. echo hs(3) . implode(hs(3), $test_array);
  64.  
  65. echo hs(2) . tab() . '<br>' . hs(2);
  66.  
  67. echo hs(1, "\r") . implode(hs(1, "\r"), $test_array);
  68. echo hs(1, "\n") . implode(hs(1, "\n"), $test_array);
  69. echo hs(1, "\r\n") . implode(hs(1, "\r\n"), $test_array);
  70.  
  71. echo hs(2) . tab() . '<br>' . hs(2);
  72.  
  73. echo hs() . tab() . implode(hs() . tab(), $test_array);
  74. echo hs() . tab(2) . implode(hs() . tab(2), $test_array);
  75. echo hs() . tab(3) . implode(hs() . tab(3), $test_array);
  76.  


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;
 
  1. // Make fake db info
  2. $fake_db = array(
  3.       array("link_id" => '1', "link" => 'http://www.ozzu.com', "source" => 'Google'),
  4.       array("link_id" => '2', "link" => 'http://www.ozzu.com', "source" => 'Google'),
  5.       array("link_id" => '3', "link" => 'http://www.ozzu.com', "source" => 'Google'),
  6.       array("link_id" => '4', "link" => 'http://www.ozzu.com', "source" => 'Google'),
  7.       array("link_id" => '5', "link" => 'http://www.ozzu.com', "source" => 'Google'),
  8.       array("link_id" => '6', "link" => 'http://www.ozzu.com', "source" => 'Google'),
  9.       array("link_id" => '7', "link" => 'http://www.ozzu.com', "source" => 'Google'),
  10.       array("link_id" => '8', "link" => 'http://www.ozzu.com', "source" => 'Google'),
  11.       array("link_id" => '9', "link" => 'http://www.ozzu.com', "source" => 'Google'),
  12.       array("link_id" => '10', "link" => 'http://www.ozzu.com', "source" => 'Google')
  13.       );
  14.  
  15. // Make the table array
  16. $table = array();
  17.  
  18. // Build the table header
  19. $table[] = '<table>';
  20. $table[] = "\t" . '<thead>';
  21. $table[] = "\t\t" . '<tr>';
  22. $table[] = "\t\t\t" . '<th>ID</th>';
  23. $table[] = "\t\t\t" . '<th>Link</th>';
  24. $table[] = "\t\t\t" . '<th>Source</th>';
  25. $table[] = "\t\t" . '</tr>';
  26. $table[] = "\t" . '</thead>';
  27. $table[] = "\t" . '<tbody>';
  28.  
  29. // Loop MySql Results (fake info for demo)
  30. foreach($fake_db as $row2) {
  31.  
  32.    // Build the tables rows
  33.    $table[] = "\t\t" . '<tr>';
  34.    $table[] = "\t\t\t" . '<td>'. $row2['link_id'] .'</td>';
  35.    $table[] = "\t\t\t" . '<td>'. $row2['link'] .'</td>';
  36.    $table[] = "\t\t\t" . '<td>'. $row2['source'] .'</td>';
  37.    $table[] = "\t\t" . '</tr>';
  38.    
  39. }
  40.    
  41. // Finish the table  
  42. $table[] = "\t" . '</tbody>';
  43. $table[] = '</table>';
  44.  
  45. // Implode the table
  46. $table = implode("\r\n", $table) . "\r\n";
  47.  
  48. // Spit out the table
  49. echo $table;
  50.  
  51.  
  52. // Make the table array
  53. $table = array();
  54.  
  55. // Build the table header
  56. $table[] = '<table>';
  57. $table[] = tab() . '<thead>';
  58. $table[] = tab(2) . '<tr>';
  59. $table[] = tab(3) . '<th>ID</th>';
  60. $table[] = tab(3) . '<th>Link</th>';
  61. $table[] = tab(3) . '<th>Source</th>';
  62. $table[] = tab(2) . '</tr>';
  63. $table[] = tab() . '</thead>';
  64. $table[] = tab() . '<tbody>';
  65.  
  66. // Loop MySql Results (fake info for demo)
  67. foreach($fake_db as $row2) {
  68.  
  69.    // Build the tables rows
  70.    $table[] = tab(2) . '<tr>';
  71.    $table[] = tab(3) . '<td>'. $row2['link_id'] .'</td>';
  72.    $table[] = tab(3) . '<td>'. $row2['link'] .'</td>';
  73.    $table[] = tab(3) . '<td>'. $row2['source'] .'</td>';
  74.    $table[] = tab(2) . '</tr>';
  75.    
  76. }
  77.    
  78. // Finish the table  
  79. $table[] = tab() . '</tbody>';
  80. $table[] = '</table>';
  81.  
  82. // Implode the table
  83. $table = implode(hs(), $table) . hs();
  84.  
  85. // Spit out the table
  86. echo $table;
  87.  


With all of the examples I've displayed the script completes in 0.00025200843811035 microseconds
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • gophw1
  • Newbie
  • Newbie
  • gophw1
  • Posts: 12

Post 3+ Months Ago

Well that's pretty neat!

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 148 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.