Skip to content

Instantly share code, notes, and snippets.

@pushandplay
Created October 3, 2013 11:19
Show Gist options
  • Save pushandplay/6808202 to your computer and use it in GitHub Desktop.
Save pushandplay/6808202 to your computer and use it in GitHub Desktop.
<?php
function parseFavicon($html) {
// 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" />
preg_match('/<link.*?rel=("|\').*icon("|\').*?href=("|\')(.*?)("|\')/i', $html, $matches);
if (count($matches) > 4) {
return trim($matches[4]);
}
// Order of attributes could be swapped around: <link type="image/png" href="http://example.com/icon.png" rel="icon" />
preg_match('/<link.*?href=("|\')(.*?)("|\').*?rel=("|\').*icon("|\')/i', $html, $matches);
if (count($matches) > 2) {
return trim($matches[2]);
}
// No match
return null;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment