I get a page using file_get_contents
from a remote server, but I want to filter that page and get a DIV from it that has class "text" using PHP. I started with DOMDocument
but I'm lost now.
Any help?
$file = file_get_contents("xx");
$elements = new DOMDocument();
$elements->loadHTML($file);
foreach ($elements as $element) {
if( !is_null($element->attributes)) {
foreach ($element->attributes as $attrName => $attrNode) {
if( $attrName == "class" && $attrNode== "text") {
echo $element;
}
}
}
}
you can use simple_html_dom like here simple_html_dom doc
or use my code like here :
echo the $con_div in plaintext..
it's mean you will find the first div in array ('div',0) and show it in plaintext.. i hope it help you :cheer
Once you have loaded the document to a
DOMDocument
instance, you can use XPath queries on it -- which might be easier than going yourself through the DOM.For that, you can use the
DOMXpath
class.For example, you should be able to do something like this :
(Not tested, so you might need to adapt the XPath query a bit...)
Personally, I like Simple HTML Dom Parser.
Pretty simple, huh? It accommodates selectors like jQuery :)