0

I'm trying to fetch data from html document.

<table style="border:0px none;margin:0px;padding:0px"><tr><td valign="middle"><p style="background-color:#EE0000;color:#fff;margin-right:5px;padding: 2px 3px;"><b>7767</b></p></td><td valign="middle"><h2 style="padding:1px">title 2</h2></td></tr></table>

there are undeterminated number of tables in the page like this one , every time when there are a table I want to get the number after the style, here 7767 , and the sentence in the second , here title 2. I found some examples I tried to adjust them but it still don't work

$html = file_get_html('http://website.com/');


foreach($html->find('table') as $article) {
    $item['nummer']     = $article->find('td.b', 0)->plaintext;
    $item['title']    = $article->find('td.h2', 0)->plaintext;
    $articles[] = $item;
}
3
  • td.b means a TD tag with class="b", and td.h2 means a TD tag with class="h2". Why are you specifying classes in your selectors when your data doesn't have those classes? Commented Apr 18, 2014 at 20:22
  • 1
    @TheGuywithTheHat: Stay trendy. Commented Apr 18, 2014 at 20:23
  • 1
    @TheGuywithTheHat I fixed that :) Commented Apr 18, 2014 at 20:23

1 Answer 1

1

. is for specifying classes. If you want to specify that one element is inside another, separate them with a space:

$item['nummer']     = $article->find('td b', 0)->plaintext;
$item['title']    = $article->find('td h2', 0)->plaintext;

The syntax for the argument to find is mostly the same as CSS selectors.

Sign up to request clarification or add additional context in comments.

2 Comments

I thought about mentioning it, but I don't think it's necessary in this case.
true, just to avoid another question about it ^_^

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.