I have the contents of a web page assigned to a variable $html
Here’s an example of the contents of $html:
<div class="content">something here</div>
<span>something random thrown in <strong>here</strong></span>
<div class="content">more stuff</div>
How, using PHP can I create an array from that that finds the contents of <div class="content"></div> regions like this (for the example above) so:
echo $array[0] . "\n" . $array[1]; //etc
outputs
something here
more stuff
Assuming this is just a simplified case in the OP and the real situation is more complicated, you’ll want to use XPath.
If it’s really complex, then you may want to use DOMDocument (with DOMXPath), but here’s a simple example using SimpleXML
Since you explicitly asked about creating an array for this, you could use:
and
$res_Arrwould be an array with the contents you’re looking for.See http://php.net/manual/en/simplexmlelement.xpath.php for php SimpleXML Xpath info and http://www.w3.org/TR/xpath for the XPath specifications