Search code examples
phpdomdomdocumenthtml-content-extraction

Extracting description out of an html page


I am trying to extract title and description out of web pages, using DOMdocument(), I am successful in extracting title like this

$d=new DOMDocument();
$d->loadHTML($html);
$title=$d->getElementsByTagName("title")->item(0)->textContent;

I can extract the description by looping through all meta tags and checking for the name="desctiption"attribute but looping makes the process slow so wanted to know if there can be a direct method for extracting content using some attribute selector in php DOMdocument??


Solution

  • I don't think this can be done with DOMDocument alone, but it is possible in combination with with DOMXPath:

    $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Dom - Xpath test</title>
    <meta name="description" content="The first description meta tag" />
    <meta name="keywords" content="none, no-keywords" />
    <meta name="description" content="the second description tag" />
    </head>
    <body>
    <p>This is the test HTML</p>
    </body>
    </html>
    ';
    
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $domx = new DOMXPath($dom);
    $desc = $domx->query("//meta[@name='description']");
    
    $i = 0;
    while ($item = $desc->item($i++)) {
        echo '<p>'.$item->getAttribute('content').'</p>';
    }