Pour l'amour des espaces blancs

  • ScottG
  • Proficient
  • Proficient
  • No Avatar
  • Inscription: Juil 06, 2010
  • Messages: 260
  • Status: Offline

Message Janvier 3rd, 2013, 9:58 am

Une chose qui me dérange le plus est regardant code ou affichant la source de la sortie d'une page Web et qui ont besoin de le formater avant de commencer à essayer de réparer. Quand j'écris code j'utilise beaucoup d'espace blanc et essayer de commenter chaque ligne ainsi que si votre ont été pour vous emmener le code devrait être en mesure de savoir ce que doit faire le script. Je fais aussi le code source de la sortie mise en forme n'est plus facile à lire et à déboguer si les choses ne vont pas correctement.

Alors pensé Id Id partager quelques fonctions que j'ai écrit pour me dépanner. Ils sont fondamentalement inutiles en plus le facteur de mise en forme du code source à lire plus facilement.

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.  


Avec ces deux fonctions, ils peuvent transformer quelque chose comme
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.  


Dans
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.  


Exemple d'utilisation
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.  


Je sais que je pourrais simplement coder les espaces difficiles et onglets quand faire la sortie, mais qui peut commencer à obtenir malpropre, comme ce qui suit
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.  


Avec tous les exemples Ive affichée le script terminé (en microsecondes) 0.00025200843811035
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Janvier 3rd, 2013, 9:58 am

  • gophw1
  • Newbie
  • Newbie
  • No Avatar
  • Inscription: Déc 11, 2012
  • Messages: 12
  • Status: Offline

Message Janvier 3rd, 2013, 5:51 pm

C'est bien assez propre !

Afficher de l'information

  • Total des messages de ce sujet: 2 messages
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 132 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC