1

Using Zend_Dom_Query, I would like to retrieve meta data out of an HTML string.

to retrieve links you can simply query like so:

$results = $dom->query('a'); //Where $dom is your html string

Unfortunately this doesn't seem to work with meta

$results = $dom->query('meta'); //doesn't work

How can I retrieve meta data and then filter by its 'property' attribute?

An example of what I'm looking for:

               public function meta($dom)
{
    $results = $dom->query('meta'); //This is not a correct query (does anyone have an alternative?)
    $links = array();
    foreach ($results as $index => $result) {
        if ($result->getAttribute('property') == 'title') { //find <meta property="title"
            echo $result->getAttribute('content') . '<br />'; //echo the content attribute of the title
        }
    }
    return $results;
}

This code would work once the query is correct. However I would like to take it a step further and query directly for <meta property="title" content="This is the Title" /> instead of retrieving all meta and looping around to get the right one.

Any help with either getting all meta data using zend_dom_query or (more importantly) querying to receive only the meta data where property == title would be appreciated.

Thanks

David Sigley
  • 1,126
  • 2
  • 13
  • 28

2 Answers2

0

the meta tag in not a valid CSS selector so you have to use the $dom->queryXpath($xPathQuery) method instead of $dom->query().

maybe something like:

$dom->queryXpath('/html/head');

I'm not sure of the exact query string to use, but this is the idea.

Zend_Dom_Query Theory of Operation.

RockyFord
  • 8,529
  • 1
  • 15
  • 21
0

if you have got url try this:


$metatagarray = get_meta_tags($url);
if (!empty($metatagarray["keywords"]))
$metakey = $metatagarray["keywords"];
if (!empty($metatagarray["description"]))
$metadesc = $metatagarray["description"];

3ehrang
  • 619
  • 1
  • 7
  • 22