Skip to content

Instantly share code, notes, and snippets.

@onigetoc
Last active November 1, 2016 21:00
Show Gist options
  • Save onigetoc/45fcd8b3ec98bcc10b850b1b6a33002d to your computer and use it in GitHub Desktop.
Save onigetoc/45fcd8b3ec98bcc10b850b1b6a33002d to your computer and use it in GitHub Desktop.
Get favicon V1
<?php
// REGEX LIST
// $success = preg_match('/\<link.*href=\"(.*)\" \/\>/is', $str, $matches);
// $success = preg_match('/\<link.*href=\"(.*)\" \/\>/is', $text, $matches);
echo "<h2>Find RSS Feed URLs</h2>";
//$url = "https://mrmondialisation.org";
$url = "http://stackoverflow.com/";
$favicon = iconprovider($url);
echo $favicon;
function iconprovider($url) { // Find RSS feed in a web page url - a litle bit slower
$icons = "";
//$subject = file_get_contents($url);
$subject = getContent($url);
// serach for all 'RSS Feed' declarations
if (preg_match_all('#<link[^>]+rel=\s*(?:"|)(icon|shortcut icon)[^>]*>#is', $subject, $rawMatches)) {
// extract url from each declaration
foreach ($rawMatches[0] as $rawMatch) {
if (preg_match('#href=\s*(?:"|)([^"\s>]+)#i', $rawMatch, $rawUrl)) {
$icons .= $rawUrl[1].'<br>';
//break;
}
}
}
$favicon = $icons;
//$favicon = str_replace("amp;", "", $favicon);
$favicon = htmlspecialchars_decode($favicon);
return $favicon;
}
function getContent($url) { // Find RSS feed in a web page url - a litle bit slower
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, urlencode($url));
$content = curl_exec($ch);
if( ($content = curl_exec($ch) ) === false)
{
$content = @file_get_contents($url);
return $content ;
}
else
{
return $content ;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment