Pour l'amour des espaces blancs
- ScottG
- Proficient


- Inscription: Juil 06, 2010
- Messages: 260
- Status: Offline
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.
Avec ces deux fonctions, ils peuvent transformer quelque chose comme
Dans
Exemple d'utilisation
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
Avec tous les exemples Ive affichée le script terminé (en microsecondes) 0.00025200843811035
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);
}
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);
- }
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>
- <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>
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>
<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>
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);
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);
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;
$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;
Avec tous les exemples Ive affichée le script terminé (en microsecondes) 0.00025200843811035
- Anonymous
- Bot


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Janvier 3rd, 2013, 9:58 am
Page 1 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
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

