gdim
gdim

Reputation: 213

Get content of a div class and store to database table using PHP

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

Answers (3)

Amit Ray
Amit Ray

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

Neel Thakkar
Neel Thakkar

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

John Wedgbury
John Wedgbury

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

Related Questions