1
  <td class="cinetime">
    <div>Screen: 3</div>
    <br clear="all">
    <span>1:00</span>
    <span>11:00</span>
    <span>13:00</span>
    <span>15:00</span>
    <br clear="all">
    <div>Screen: 4</div>
    <br clear="all">
    <span>12:05</span>
    <span>14:05</span>
    <span>16:05</span>
    <span>18:05</span>
    <span>20:05</span>
    <div>Screen: 3 (3D)</div>
  </td>

Above is the HTML i'm ing.

I want to take the data in <span> for screen 3 and Screen 4 separately.

This is my code below but it takes data from all the <span>s

foreach($cinema->find("td.cinetime span") as $times) {
    echo $times->plaintext;
}
carbonr
  • 6,049
  • 5
  • 46
  • 73
  • 1
    *(related)* http://stackoverflow.com/questions/4979836/noob-question-about-domdocument-in-php/4983721#4983721 – Gordon Oct 26 '12 at 12:13

2 Answers2

0

You need to go over all elements inside td.cintime and check if text is what you need (like Screen: 3) and then check if other element is span, then put it in some array till you get another div. It means one screen block is over. Repeat till you get everything you need.

Pseudo code:

$screensData = array();
$start = false;
foreach($cinema->find("td.cintime *") as $times) {
   if($times->tag == 'div') {
      $start = false;
      if($times->plaintext == 'Screen: 3') {
         $screensData[3] = array();
         $start = 3;
      }
   }

   if($start && $times->tag == 'span') {
      $screensData[$start][] = $times->plaintext;
   }
}
kasp3r
  • 310
  • 1
  • 13
  • there is no function called "element" in http://simplehtmldom.sourceforge.net/manual.htm. – carbonr Oct 26 '12 at 13:15
  • that is the key kasp3r. Thanks for helping but i can't seem to find any function to get me this – carbonr Oct 26 '12 at 13:43
  • 1
    You need to look into it more carefully. By spending 5 seconds in the manual you gave, I found the answer in last example. Use $times->tag . Pseudo code is also updated. – kasp3r Oct 26 '12 at 14:26
0

Something like this:

$dom = new simple_html_dom();
$dom->load($source);


$cinetime = $dom->find('.cinetime',0);

$screens = array();
$screenName = '';

foreach($cinetime->children() as $elem){
    if($elem->tag == 'div'){
        $screenName = $elem->innertext;
    } else
    if($elem->tag == 'span'){
        $screens[$screenName][] = $elem->innertext;
    }
}
print_r($screens);

That produces this:

Array
(
[Screen: 3] => Array
    (
        [0] => 1:00
        [1] => 11:00
        [2] => 13:00
        [3] => 15:00
    )

[Screen: 4] => Array
    (
        [0] => 12:05
        [1] => 14:05
        [2] => 16:05
        [3] => 18:05
        [4] => 20:05
    )

)

?

EJTH
  • 2,178
  • 1
  • 20
  • 25
  • this is great, the '$elem->tag' was the key i was looking for. I have voted kasp3r for pointing this out. Thank you very much guys – carbonr Oct 26 '12 at 23:17