Question

I am trying to get the meta tags from websites and echo the result, it is working well so far, but I found an external problem...

I am using this code:

static private function _parse($HTML) {
    $old_libxml_error = libxml_use_internal_errors(true);

    $doc = new DOMDocument();
    $doc->loadHTML($HTML);

    libxml_use_internal_errors($old_libxml_error);

    $tags = $doc->getElementsByTagName('meta');
    if (!$tags || $tags->length === 0) {
        return false;
    }

    $page = new self();

    foreach ($tags AS $tag) {
        if ($tag->hasAttribute('property') &&
            strpos($tag->getAttribute('property'), 'og:') === 0) {
            $key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
            $page->_values[$key] = $tag->getAttribute('content');
        }
    }


    if (empty($page->_values)) { return false; }

    return $page;
}

When the URL that I'm trying to fetch has the meta tags using the attibute 'property' (Ex: <meta property="title">) everything goes fine, as the code is suppose to do... but some URLs uses the attribute 'name' (Ex: <meta name="title">) instead 'property'...

So, I need to find a solution to extract the values from 'property' and if it's empty, return the values from 'name'...

I tried some things, but do not succed so far.. Anyone have ideas?

Was it helpful?

Solution

Maybe this

foreach ($tags AS $tag) {
    if ($tag->hasAttribute('property') &&
        strpos($tag->getAttribute('property'), 'og:') === 0) {
        $key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
        $page->_values[$key] = $tag->getAttribute('content');
    } else if ($tag->hasAttribute('name') &&
        strpos($tag->getAttribute('name'), 'og:') === 0) {
        $key = strtr(substr($tag->getAttribute('name'), 3), '-', '_');
        $page->_values[$key] = $tag->getAttribute('content');
    }
}

As OZ_ noticed, you could use str_ireplace to rename all meta name to meta property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top