Sushi
Sushi

Reputation: 676

How to use php simpleHTMLdom to parse HTML an get strings?

how to use simpleHTMLdom to get a string that is inside a <th> tag (and there is many other <th>) For example :

<th>name</th>
    <td>john</td>
<th>age</th>
    <td>32</td>

How to get 32 ? note that sometimes, the website i'm parsing, doesn't contain the same number of <th> sometimes there is no <th>name</th> so i cant do : $html->find('th',1) to get age and go to <td> to get 32

Any way to perform something like : $html->find('age') ?

Thank you

Upvotes: 1

Views: 60

Answers (1)

Naqash Malik
Naqash Malik

Reputation: 1816

You have to do something like this:

$ageIndex = 0;
foreach($html->find('th') as $key => $val)
{
  if($val->innertext == 'age')
  {
     $ageIndex = $key;
     break;
  }
}

$age = $html->find('td',$ageIndex);

Upvotes: 1

Related Questions