Reputation: 550
I have a rather large HTML document that I am trying to extract information from. I have gotten as far as figuring out that I probably need to use a DOMDocument object, and xPath. I need to return the contents of a specific DIV. The good news is that it has a class tag associated with it. The bad news is that it is buried in some non-specific location in the HTML document, within several layers of othere DIV's, and that location may change. So, what I am looking to return the contents of the div.
<div class='target'>Return all of this stuff</div>
The trick seems to be in that I don't know the particular location that this particular div will be in. I need a way to just say 'search the entire DOM for the div with the class-name of target'. there may be multiple coinsurance, but probably not. However, once I get the, probably array of element contents, I can take it from there. And again, using PHP 5.4.
Upvotes: 0
Views: 191
Reputation: 943
If I understand correctly, this can be accomplished with native php string handling alone.
Have you tried something along the lines of.
$start = stripos($STRINGYOUARESEARCHING, "<div class='target'>")
$end= stripos($STRINGYOUARESEARCHING, "</div>",start);
$contents= substr($STRINGYOUARESEARCHING, $start, $end-$start)
Let me know if this isn't the kind of answer you're looking for... here I assume you have a large block of HTML with your target in it?
(Note: You may have to offset $end or $start by strlen(<div class='target'>)
Upvotes: 0
Reputation: 14730
The xpath query you need is:
$query = "//div[@class='target']";
which you can use with a DOMXPath
object by invoking the query
method.
Upvotes: 2