Remove tags from a varchar php variable

0

I have a varchar php variable that has an html. It consists of a table, in that table there is a class "disappears", I wanted all tags that had this class to be taken from the php variable.

Before:

  $varaiavel = "
 <table>
  <tr class="header">
      <td class="check"></td>
      <td class="campo_grid_1 desaparecer">Código</td>
      <td class="campo_grid_2" style="width:30%">Nome do professor</td>
      <td class="campo_grid_3" style="width:30%">Coordenação</td>
     <td class="campo_grid_4" style="width:10%">Telefone</td>
      <td class="campo_grid_5" style="width:20%">Email</td>
  </tr> 
  <tr>
      <td class="check"></td>
      <td class="campo_grid_1 desaparecer">12</td>
      <td class="campo_grid_2" style="width:30%">Paulo Rodolfo</td>
      <td class="campo_grid_3" style="width:30%">(63)33131221</td>
     <td class="campo_grid_4" style="width:10%">T(63)33131221</td>
      <td class="campo_grid_5" style="width:20%">paulo.webrall</td>
  </tr> 

";

Then:

 $varaiavel = "
 <table>
  <tr class="header">
      <td class="check"></td>
      <td class="campo_grid_2" style="width:30%">Nome do professor</td>
      <td class="campo_grid_3" style="width:30%">Coordenação</td>
     <td class="campo_grid_4" style="width:10%">Telefone</td>
      <td class="campo_grid_5" style="width:20%">Email</td>
  </tr> 
  <tr>
      <td class="check"></td>
      <td class="campo_grid_2" style="width:30%">Paulo Rodolfo</td>
      <td class="campo_grid_3" style="width:30%">(63)33131221</td>
     <td class="campo_grid_4" style="width:10%">T(63)33131221</td>
      <td class="campo_grid_5" style="width:20%">paulo.webrall</td>
  </tr> 

";

    
asked by anonymous 01.03.2018 / 22:48

2 answers

0

For this you will need the DomDocument and DomXPath :

<?php

$variavel = '
 <table>
  <tr class="header">
      <td class="check"></td>
      <td class="campo_grid_1 desaparecer">Código</td>
      <td class="campo_grid_2" style="width:30%">Nome do professor</td>
      <td class="campo_grid_3" style="width:30%">Coordenação</td>
     <td class="campo_grid_4" style="width:10%">Telefone</td>
      <td class="campo_grid_5" style="width:20%">Email</td>
  </tr> 
  <tr>
      <td class="check"></td>
      <td class="campo_grid_1 desaparecer">12</td>
      <td class="campo_grid_2" style="width:30%">Paulo Rodolfo</td>
      <td class="campo_grid_3" style="width:30%">(63)33131221</td>
     <td class="campo_grid_4" style="width:10%">T(63)33131221</td>
      <td class="campo_grid_5" style="width:20%">paulo.webrall</td>
  </tr> ';

$dom = new DOMDocument();
$dom->preserveWhiteSpace = true;
$dom->loadHTML($variavel);

//use DomXPath para encontrar os tds com o class name
$xpath = new DomXPath($dom);
$classname='desaparecer';
$xpath_results = $xpath->query("//td[contains(@class, '$classname')]");

//para cada match ele exclui a tag
foreach( $xpath_results as $td ) {
    $td->parentNode->removeChild($td);
}

//o documento final
echo $dom->saveHTML();
    
01.03.2018 / 23:14
1

You can use the DOMDocument and XPath classes to find and remove the nodes:

For the variable:

$variavel = '
 <table>
  <tr class="header">
      <td class="check"></td>
      <td class="campo_grid_1 desaparecer">Código</td>
      <td class="campo_grid_2" style="width:30%">Nome do professor</td>
      <td class="campo_grid_3" style="width:30%">Coordenação</td>
     <td class="campo_grid_4" style="width:10%">Telefone</td>
      <td class="campo_grid_5" style="width:20%">Email</td>
  </tr> 
  <tr>
      <td class="check"></td>
      <td class="campo_grid_1 desaparecer">12</td>
      <td class="campo_grid_2" style="width:30%">Paulo Rodolfo</td>
      <td class="campo_grid_3" style="width:30%">(63)33131221</td>
     <td class="campo_grid_4" style="width:10%">T(63)33131221</td>
      <td class="campo_grid_5" style="width:20%">paulo.webrall</td>
  </tr> ';

Picking up the document object:

$doc = new DOMDocument();
$doc->loadHTML($variavel);

Finding nodes with classname disappear:

$xpathsearch = new DOMXPath($doc);
//Buscas com XPATH precisam ser especificas
$nodes = $xpathsearch->query('//td[contains(@class,"desaparecer")]'); 
foreach($nodes as $node){
    $node->parentNode->removeChild($node);
};
$variavel_limpa = $doc->saveHTML();

$ variavel_limpa is the result you want.

Refs.:

DOMDocument

DOMXPath

Remove Node Using XPath

    
01.03.2018 / 23:09