开发者

Detecting </span> in PHP?

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, '<spa开发者_运维技巧n class="item-header">')){
   // What to do here?
 }

Any Ideas? :)


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜