1

Linked IN

No link...Use default location.
http://www.linkedin.com/favicon.ico

Twitter

  <link href="/phoenix/favicon.ico" rel="shortcut icon" type="image/x-icon" />

Pinterest

<link rel="icon" href="http://passets-cdn.pinterest.com/images/favicon.png" type="image/x-icon" />

Facebook

<link rel="shortcut icon" href="https://s-static.ak.facebook.com/rsrc.php/yi/r/q9U99v3_saj.ico" />

I've determined that the only 100% way to find a favicon is to check the source and see where the link is.

  • Default location is not always used. Note first 2 examples.
  • Google API works only about 85% of the time. Try It Out

Is there a function that can parse this info out? Or is there a good strategy for using a regex to pull it out manually.

I will be parsing the html server side to get this info.

Ideas:

Regex Example: Try Here. Seems to easy...but here is a starting point.

<link\srel="[Ss]hortcut [Ii]con"\shref="(.+)"(.+)>

3 Answers3

3

Use a parser:

$dom = new DOMDocument();
@$dom->loadHTML($input);
$links = $dom->getElementsByTagName('link');
$l = $links->length;
$favicon = "/favicon.ico";
for( $i=0; $i<$l; $i++) {
    $item = $links->item($i);
    if( strcasecmp($item->getAttribute("rel"),"shortcut icon") === 0) {
        $favicon = $item->getAttribute("href");
        break;
    }
}
// You now have your $favicon
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Alternative to PHP 5 DOMDocument: raw regex

This works for all cases so far.

    $pattern = '#<link\s+(?=[^>]*rel="(?:shortcut\s)?icon"\s+)(?:[^>]*href="(.+?)").*>#i';      
  • And what happens if I put the `href` attribute *before* the `rel`, hmm? [The pony he comes...](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Niet the Dark Absol May 23 '12 at 18:10
  • This works for all cases so far. Much simpler then parsing the whole document. + I know how it works. –  May 29 '12 at 15:56
0

You will have to work around several issues, like site redirects and various caveats. Here is what I did to harvest something like 90% of my websites feeds favicons:

<?
/*
  nws-favicon : Get site's favicon using various strategies

  This script is part of NWS
  https://github.com/xaccrocheur/nws/

*/


function CheckImageExists($imgUrl) {
    if (@GetImageSize($imgUrl)) {
        return true;
    } else {
        return false;
    };
};

function getFavicon ($url) {

$fallback_favicon = "/var/www/favicon.ico";

    $dom = new DOMDocument();
    @$dom->loadHTML($url);
    $links = $dom->getElementsByTagName('link');
    $l = $links->length;
    $favicon = "/favicon.ico";
    for( $i=0; $i<$l; $i++) {
        $item = $links->item($i);
        if( strcasecmp($item->getAttribute("rel"),"shortcut icon") === 0) {
            $favicon = $item->getAttribute("href");
            break;
        }
    }

    $u = parse_url($url);

    $subs = explode( '.', $u['host']);
    $domain = $subs[count($subs) -2].'.'.$subs[count($subs) -1];

    $file = "http://".$domain."/favicon.ico";
    $file_headers = @get_headers($file);

    if($file_headers[0] == 'HTTP/1.1 404 Not Found' || $file_headers[0] == 'HTTP/1.1 404 NOT FOUND' || $file_headers[0] == 'HTTP/1.1 301 Moved Permanently') {

        $fileContent = @file_get_contents("http://".$domain);

        $dom = @DOMDocument::loadHTML($fileContent);
        $xpath = new DOMXpath($dom);

        $elements = $xpath->query("head/link//@href");

        $hrefs = array();

        foreach ($elements as $link) {
            $hrefs[] = $link->value;
        }

        $found_favicon = array();
        foreach ( $hrefs as $key => $value ) {
            if( substr_count($value, 'favicon.ico') > 0 ) {
                $found_favicon[] = $value;
                $icon_key = $key;
            }
        }

        $found_http = array();
        foreach ( $found_favicon as $key => $value ) {
            if( substr_count($value, 'http') > 0 ) {
                $found_http[] = $value;
                $favicon = $hrefs[$icon_key];
                $method = "xpath";
            } else {
                $favicon = $domain.$hrefs[$icon_key];
                if (substr($favicon, 0, 4) != 'http') {
                    $favicon = 'http://' . $favicon;
                    $method = "xpath+http";
                }
            }
        }

        if (isset($favicon)) {
            if (!CheckImageExists($favicon)) {
                $favicon = $fallback_favicon;
                $method = "fallback";
            }
        } else {
            $favicon = $fallback_favicon;
            $method = "fallback";
        }

    } else {
        $favicon = $file;
        $method = "classic";

        if (!CheckImageExists($file)) {
            $favicon = $fallback_favicon;
            $method = "fallback";
        }

    }
    return $favicon;
}

?>
yPhil
  • 8,049
  • 4
  • 57
  • 83