1

I'm using PHP simple dom parser, says my markup is like this

<table class="tb">..</table>
<table>..</table>
<table>..</table>

I tried

$banner = '<div class="ads">blablabla</div>';
foreach($html->find('tb') as $table){
    $table->outertext .= $banner;
}

but it loops all the table. I tried find('div[class="tb"][1]') but it doesn't work. I want a div appear only once btw the first and second table.

AyB
  • 11,609
  • 4
  • 32
  • 47
user3522725
  • 387
  • 1
  • 4
  • 13

1 Answers1

0

First of all, you are trying to find div with class tb whereas you should be looking for the table element. Next, to 'append' the existing text with the $banner value, you need to use .= operator so that your table code is not erased. And then finally, you save the changes. The code should look like:

foreach($html->find('table[class=tb]') as $element){
       echo $element->src . '<br>';
       $element->outertext .= $banner;
}

$html->save();

The output would be:

<table class="tb">..</table><div class="ads">banner</div>  <table>..</table>  <table>..</table>
AyB
  • 11,609
  • 4
  • 32
  • 47
  • what happen if I don't save the it? because in the end I echo $html – user3522725 Jun 10 '14 at 09:23
  • @user3522725 As per [here](http://simplehtmldom.sourceforge.net/manual_api.htm), looks like save() is used when you want to save the contents into a file, for your case, you can go ahead without it. – AyB Jun 10 '14 at 09:28
  • says I've 5 table, I used ur code, it has 5 banner within them.. that's the problem – user3522725 Jun 10 '14 at 18:19
  • @user3522725 Could you [pastebin](http://pastebin.com) the structure of those 5 table? – AyB Jun 11 '14 at 04:19