Remove tags from a php varchar variable

Asked

Viewed 49 times

0

I have a php varchar variable that has an html. There is a table, in this table in the 'td' there is a class "disappear", I wanted all the tags that possessed this class to be removed 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> 

";

Afterward:

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

";

2 answers

1

You can use the Domdocument and Xpath classes to find and remove the nodes (nodes):

To 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> ';

Taking the object of the document:

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

Locating 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();

$variable_clean is the result you want.

Refs.:

Domdocument

Domxpath

Remove Node Using Xpath

0


For this you will need the classes 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.