0

How can remove all the div tags with the class attribute equal to info in a PHP page, i.e. the <div class="info">...</div> tags need to be removed from an HTML string.

I have seen this question, but I want to remove elements with the class equal to info.

I will like to use Simple HTML DOM (compared to any other library) because I am already using it in the program.

Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183
  • have you looked into using javascript for this? and by remove do you want it to not even show up in the inspector? or just not display? – iam-decoder Aug 13 '14 at 22:46
  • 1
    [From the manual](http://simplehtmldom.sourceforge.net/manual.htm) you should be able to access class="info" like this: `$ret = $html->find('div[class=info]');`. Also, `->find('.info')` for the class only, or `->find('div.info')` – scrowler Aug 13 '14 at 22:47
  • @iamde_coder I thought about it but I need to avoid JS as much as possible. If it is not getting done this way, I'll end up using JS though. About "remove", it's better if it is there but is not just displayed. – Solace Aug 13 '14 at 22:49
  • 1
    See [this answer](http://stackoverflow.com/a/8227616/2812842) for how to remove the elements – scrowler Aug 13 '14 at 22:51
  • @scrowler I checked that. I don't know what `$e` is in `$e->outertext='';` is? – Solace Aug 13 '14 at 22:53
  • 1
    `$e` is the element -- e.g. in a query like this: `foreach ($html->find('div.info') as $e)` or `$e = $html->find('div.info',0)` – i alarmed alien Aug 14 '14 at 06:29

1 Answers1

2

There are multiple ways to select elements by class. Check out the manual.

$html->find('div[class=info]');
$html->find('div.info');
$html->find('.info'); // not just divs

Then using this answer, you can remove the elements by setting outertext to be blank. In a loop it might look like this:

foreach($html->find('div.info') as $node) {
    $node->outertext = '';
}

Edit: this is a good article which seems to cover most aspects of manipulation and common pitfalls (including deleting data).

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92