0

I have a script that checks the source of another page, I'm having my code search for

 <span class="item-header">

in the source. When it finds it, I wan't to print everything that is in the span, but I am not sure how I would do that in PHP? How do I check when to stop printing everything after the span until it finds </span> Here is my code:

 if (stristr($source, '<span class="item-header">')){
   // What to do here?
 }

Any Ideas? :)

Alex
  • 1,398
  • 4
  • 15
  • 19

2 Answers2

2

You could use regex, but people will caution against parsing HTML with regex. I would recommend using DOMDocument to parse the HTML and DOMXPath to query the document tree. Try this:

$dom = new DOMDocument();
@$dom->loadHTML($page);
$dom_xpath = new DOMXPath($dom);
$entries = $dom_xpath->evaluate("//span[@class='item-header']");
foreach ($entries as $entry) {
    print $entry->nodeValue;
}
Rusty Fausak
  • 7,355
  • 1
  • 27
  • 38
  • Thank you - but when I tested this, nothing printed. I set $page to the URL of the page. – Alex Sep 24 '11 at 00:09
  • Nevermind, I set $page as the source and it worked perfectly. Thanks! – Alex Sep 24 '11 at 00:11
  • @Alex: if you find this question is match to your problem, mark it as accepted: http://stackoverflow.com/faq#howtoask – ariefbayu Sep 24 '11 at 09:21
1

You would likely be better off using an actual parser instead of regex-based searches. That way you could grab the node for the span and get the text value.

jricher
  • 2,663
  • 1
  • 18
  • 17