Parse a HTML document and get a specific element in PHP and save its HTML -
all want save first div attribute role="main" string external url using php.
so far have this:
$doc = new domdocument(); @$doc->loadhtmlfile("http://example.com/"); $xpath = new domxpath($doc); $elements = $xpath->query('//div[@role="main"]'); $str = ""; if ($elements->length > 0) { $str = $elements->item(0)->textcontent; } echo htmlentities($str);
but unfortunately $str not seem displaying html tags. text.
you can html via savehtml()
method.
$str = $doc->savehtml($elements->item(0));
Comments
Post a Comment