Supprimer un tr dans une table si td contient un texte précis

Signaler
Messages postés
5
Date d'inscription
dimanche 27 mai 2007
Statut
Membre
Dernière intervention
30 avril 2017
-
 Utilisateur anonyme -
Bonjour,

Voici mon problème après plusieurs tentatives infructueuses!

Je cherche à nettoyer le contenu d'une table. Le but est de travailler un code html de manière a ce que toutes les ligne de cette table soient supprimées si le champs Quantité vaut " 0"!

Je pense être près du but mais je ne parviens pas a mes fins!

Si quelqu'un peut m'aider?

<?php
$table = <<< EOD
<table style="width: 100%;" class="formTableLayout" border="0" id="form_24_page_0">

   
 <tbody>
 
    <tr>
        <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:left;vertical-align:middle;"> FR: <strong><span style="color: #FF7238;">Sacs (emb de 10 sacs)</span></strong><br> DE:<strong><span style="color: #FF7238;">Säcke (Pack 10 Stück)</span></strong><br> </td>
     <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> <img src="http://domaine.xyz/images/poubelle.jpg " alt="Sacs" width="36"> 
        </td>
        <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> <strong>110l</strong><br><strong>-</strong>
        </td>
        <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;">  0 
        </td>
        <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> -  
        </td>
        <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> Infos utiles / Wichtige Informationen<br><strong>-</strong><br>
        </td>
     </tr>
     <tr>
         <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:left;vertical-align:middle;"> FR: <strong><span style="color: #FF7238;">Bac</span></strong><br> DE: <strong><span style="color: #FF7238;">Behälter)</span></strong><br> </td>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> <img src="http://domaine.xyz/images/Bac.jpg " alt="Bac" width="36"> </td>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> <strong>-</strong><br><strong>-</strong></td>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;">  0 </td>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> -  </td>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> Infos utiles / Wichtige Informationen<br><strong> - </strong><br></td>
   </tr>
   <tr>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:left;vertical-align:middle;"> FR: <strong><span style="color: #FF7238;">Carton</span></strong><br> DE: <strong><span style="color: #FF7238;">Karton</span></strong><br> </td><td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> <img src="http://domaine.xyz/images/Carton.jpg " alt="Carton" width="36"> </td>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> <strong> - </strong><br><strong>-</strong></td>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;">  1 </td>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> -  </td>
   <td style="padding:5px;border:1px solid#9b9f8f;font-size:10pt;text-align:center;vertical-align:middle;"> Infos utiles / Wichtige Informationen<br><strong> - </strong><br>
   </td>
   </tr>
  </tbody></table>
EOD;
echo $table;
$dom = new \DOMDocument();
$dom->loadHTML($table);
$xpath = new \DOMXPath($dom);

// loop all <tr> element.
foreach ($xpath->query('//tr') as $tr) {
    $tds = $tr->getElementsByTagName('td');
    // get total <td> in this <tr>
    $total_item = $tds->length;

    // loop to all <td> and check.
    for ($i = 0; $i <= ($total_item-1); $i++) {
        // get table cell value.
        $table_cell_value = $tds->item($i)->nodeValue;
        preg_match('/\[  0 ]/', $table_cell_value, $charge_id);

        // check if condition is met, the charge_id is 13.
        if (is_array($charge_id) && array_key_exists(1, $charge_id) && $charge_id[1] == " 0") {
            // condition met. charge_id is 13. remove this table row.
            echo "passeici";
            $tr->parentNode->removeChild($tr);
        }
       

        unset($charge_id, $table_cell_value);
    }

    unset($tds, $total_item);
}

// get the result
$saved_html = preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $dom->saveHTML());
echo $saved_html;
?>


le code d'origirne provien d'ici : http://stackoverflow.com/questions/28406611/how-to-remove-table-row-if-condition-met-in-table-cell-preg-replace-or-domdoc/43575437#43575437

Merci pour votre aide

1 réponse


Si la colonne quantité c'est la quatrième, pourquoi ne pas la sélectionner directement au lieu de vérifier chaque ligne une à une?
J'ai réussi à obtenir ce que tu veux comme ça:

// loop all <tr> element.
foreach ($xpath->query('//tr') as $tr) {
    $tds = $tr->getElementsByTagName('td');
    // get total <td> in this <tr>
    $total_item = $tds->length;

    // get table cell value.
    $table_cell_value = $tds->item(3)->nodeValue;
    //var_dump(trim($table_cell_value));
    preg_match('/[0]/', trim($table_cell_value), $charge_id);

	var_dump($charge_id, $table_cell_value);

    // check if condition is met, the charge_id is 13.
    if (!empty($charge_id) && $charge_id[0] == "0") {
        // condition met. charge_id is 13. remove this table row.
        echo "passeici";
        $tr->parentNode->removeChild($tr);
    }
   

    unset($charge_id, $table_cell_value);

    unset($tds, $total_item);
}