Skip to content

Instantly share code, notes, and snippets.

@forestrf
Last active December 24, 2015 08:39
Show Gist options
  • Save forestrf/6771547 to your computer and use it in GitHub Desktop.
Save forestrf/6771547 to your computer and use it in GitHub Desktop.
<?php
function parseFavicon($html) {
$s = '[ \t\r\n]*?';
// Get the 'href' attribute value in a <link rel="icon" ... />
// Also works for IE style: <link rel="shortcut icon" href="http://www.example.com/myicon.ico" />
// And for iOS style: <link rel="apple-touch-icon" href="somepath/image.ico">
$matches = array();
// Search for <link rel="icon" type="image/png" href="http://example.com/icon.png" />
;
if (preg_match("/<link{$s}rel{$s}={$s}[\"'](?:shortcut )?icon[\"']{$s}href{$s}={$s}[\"']([^\"']*)[\"']/i", $html, $matches)) {
return trim($matches[1]);
}
// Order of attributes could be swapped around: <link type="image/png" href="http://example.com/icon.png" rel="icon" />
if (preg_match("/<link{$s}href{$s}={$s}[\"']([^\"']*)[\"']{$s}rel{$s}={$s}[\"'](?:shortcut )?icon[\"']/i", $html, $matches)) {
return trim($matches[1]);
}
if (preg_match("/<meta{$s}content{$s}={$s}[\"']([^\"']*)[\"']{$s}itemprop{$s}={$s}[\"']image[\"']/i", $html, $matches)) {
return trim($matches[1]);
}
// No match
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment