Reputation: 213
I am trying to get the content of a specific div class="className"
from a website and then store the content to database.I use this block of code but var_dump
shows nothing.Please help me because I am totally inexperienced in that.
Code:
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile('http://www.someLink.com');
foreach( $doc->getElementsByClassName('Classname') as $item){
$class = $item->getAttribute('div');
var_dump($class);
}
?>
Upvotes: 1
Views: 607
Reputation: 3485
I have written a small function that can create an array of the div elements found inside the class
<?php
function get_links($url,$classname) {
// Create a new DOM Document
$xml = new DOMDocument('1.0', 'UTF-8');
//To remove all unnecessary errors
$internalErrors = libxml_use_internal_errors(true);
// Load the html into the DOM
$xml->loadHTMLFile($url);
$xpath = new DOMXPath($xml);
$classes = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
for ($i = $classes->length - 1; $i > -1; $i--) {
if(!empty($classes->item($i)->firstChild->nodeValue)){
$result[] = $classes->item($i)->firstChild->nodeValue;
}
}
// Restore error level
libxml_use_internal_errors($internalErrors);
return $result;
}
$url = 'http://www.example.com';
$classname ="someclass";
$rows=get_links($url,$classname);
var_dump($rows); // YOu will get an array of the contents that you can store in database
foreach($rows as $row){
//insert DB command
}
?>
Upvotes: 0
Reputation: 406
i have created one example which is fetch content from div. This content you can easily stored in database.
$html = file_get_html('Your website');
$element = $html->find('div[id=Your id]', 0);
echo $element;
Upvotes: 0
Reputation: 181
DOMDocument->getElementsByClassName doesn't seem to be a function that exists.
Try instead using xpath like so:
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile('http://www.image-plus.co.uk/');
$finder = new DomXPath($doc);
$class_name = "green";
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class_name ')]");
$tmp_dom = new DOMDocument();
foreach ($nodes as $node)
{
$tmp_dom->appendChild($tmp_dom->importNode($node,true));
}
$innerHTML.=trim($tmp_dom->saveHTML());
echo $innerHTML;
?>
Edit: Fixed mistake
Upvotes: 2