1

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

2
  • You have to loop through all 'th' and find index of age and use that index to find its 'td'. Commented Apr 5, 2016 at 10:49
  • I agree with you @Malik but <th> (even for <td>) do not have class or id how to find index of age ? i'm looking for a way to find by string for example or something like that Commented Apr 5, 2016 at 10:52

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.