2016-07-01 5 views
5

से लाइन टूट के साथ खाली टैग निकालें मैं है निम्न 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> 

और मैं इस परिणाम के रूप में चाहते हैं:

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

सबसे आसान इसे सही ढंग से कोड करने के लिए हो सकता है, दुर्भाग्य से, यह आता है सीकेएडिटर के बहुत पुराने संस्करण से बाहर और मैं इसे अपग्रेड नहीं कर सकता (अन्य प्रभावों के कारण)।

preg_replace या रिकर्सिव फ़ंक्शन या लूप मैं खाली <div> टैग और अनियंत्रित खाली लाइनों को निकालने के लिए चला सकता हूं?

+0

लगभग simillar बात किया गया है, आप यहां देख सकते हैं http://stackoverflow.com/questions/30865464/how-to-remove-empty-html-tags-wich-containing-whitespaces-and-or-their-html- कोड –

उत्तर

1

मान लिया जाये कि आप $html नामक एक चर में इस 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; 

संपादित

पूर्ण काम कर कोड,:

<?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

उसने कुछ भी नहीं किया। – Albert

+0

@Albert मेरा संपादन देखें, जो आपके सटीक इनपुट का उपयोग करता है और अपेक्षित आउटपुट प्राप्त करता है। – smarx