scottmac / opengraph

Helper class for accessing the OpenGraph Protocol
463 stars 151 forks source link

Fix for pages with multiple copies of tags #23

Open mutatron opened 10 years ago

mutatron commented 10 years ago

A web page may have multiple og:image tags, for example, so I made each _value[$key] into an array, and then values are pushed onto the array, like so:

            if (!is_array($page->_values[$key]))
            {
                $page->_values[$key] = array();
            }
            $page->_values[$key][] = $tag->getAttribute('value');

Of course there are a few keys that need to be explicitly handled, like this:

            if (!is_array($page->_values['title']))
            {
                $page->_values['title'] = array();
            }
            $page->_values['title'][] = $titles->item(0)->textContent;
propertunist commented 10 years ago

thanks, would be great to get this integrated into the main code

pressmaster commented 9 years ago

Great idea. I went the other direction. It was only giving me the last image and I wanted the first.

    $gotImage = 0;

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

My version isn't pretty, I'm sure....