2016-07-01 24 views
5

Ho il seguente codice HTML:Rimuovere tag vuoti con interruzioni di riga da HTML

<body>Summary: <br> 
    <table class="stats data tablesorter marg-bottom"> 
     <thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead> 
     <tbody> 

      <tr> 
       <td>Team 1</td> 
       <td>95</td> 
       <td>74</td> 
       <td>0</td> 
       <td>56.21</td> 
      </tr> 

      <tr> 
       <td>Team 2</td> 
       <td>74</td> 
       <td>95</td> 
       <td>0</td> 
       <td>43.79</td> 
      </tr> 

     </tbody> 
    </table> 


<div> 
    </div> 
</body> 

E voglio questo come risultato:

<body>Summary: <br> 
    <table class="stats data tablesorter marg-bottom"> 
     <thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead> 
     <tbody> 
      <tr> 
       <td>Team 1</td> 
       <td>95</td> 
       <td>74</td> 
       <td>0</td> 
       <td>56.21</td> 
      </tr> 
      <tr> 
       <td>Team 2</td> 
       <td>74</td> 
       <td>95</td> 
       <td>0</td> 
       <td>43.79</td> 
      </tr> 
     </tbody> 
    </table> 
</body> 

più semplice sarebbe quella di codificare correttamente, purtroppo, questo viene da una versione molto vecchia di CKEditor e non posso aggiornarlo (a causa di altre implicazioni).

Che cosa è la funzione o ciclo ricorsivo preg_replace o ricorsiva posso eseguire per rimuovere i tag vuoti <div> e le righe vuote non necessarie?

+0

cosa quasi simillar è stato fatto, è può vedere qui http://stackoverflow.com/questions/30865464/how-to-remove-empty-html-tags-wich-contain-whitespaces-and-or-thethe-html- cod –

risposta

1

Supponendo di avere questo HTML in una variabile chiamata $html:

// Replace empty <div> tags with nothing 
$html = preg_replace("/<div>\s*<\/div>/", "", $html); 

// Replace multiple newlines in a row with a single newline 
$html = preg_replace("/\n+/", "\n", $html); 

echo $html; 

EDIT

codice di lavoro completa, tra cui uscita:

<?php 

$html = <<<END 
<body>Summary: <br> 
    <table class="stats data tablesorter marg-bottom"> 
     <thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead> 
     <tbody> 

      <tr> 
       <td>Team 1</td> 
       <td>95</td> 
       <td>74</td> 
       <td>0</td> 
       <td>56.21</td> 
      </tr> 

      <tr> 
       <td>Team 2</td> 
       <td>74</td> 
       <td>95</td> 
       <td>0</td> 
       <td>43.79</td> 
      </tr> 

     </tbody> 
    </table> 


<div> 
    </div> 
</body> 

END; 

// Replace empty <div> tags with nothing 
$html = preg_replace("/<div>\s*<\/div>/", "", $html); 

// Replace multiple newlines in a row with a single newline 
$html = preg_replace("/\n+/", "\n", $html); 

echo $html; 

// OUTPUT: 

// <body>Summary: <br> 
//  <table class="stats data tablesorter marg-bottom"> 
//   <thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead> 
//   <tbody> 
//    <tr> 
//     <td>Team 1</td> 
//     <td>95</td> 
//     <td>74</td> 
//     <td>0</td> 
//     <td>56.21</td> 
//    </tr> 
//    <tr> 
//     <td>Team 2</td> 
//     <td>74</td> 
//     <td>95</td> 
//     <td>0</td> 
//     <td>43.79</td> 
//    </tr> 
//   </tbody> 
//  </table> 
// </body> 

?> 
+0

Questo non ha fatto nulla. – Albert

+0

@Albert Vedere la mia modifica, che utilizza l'input esatto e ottiene l'output previsto. – smarx

Problemi correlati