Get the Favicon from a website


/ Published in: PHP
Save to your folder(s)

**Example of use:**

<a href="http://www.php.net/manual/en/index.php"><img src="<?php echo get_favicon('http://www.php.net/manual/en/index.php') ?>" alt="Favicon" title="See PHP documentation" width="16" height="16" /></a>


Copy this code and paste it in your HTML
  1. /**
  2.  * Get favicon from a URL.
  3.  *
  4.  * @author Pierre-Henry Soria <[email protected]>
  5.  * @copyright (c) 2013, Pierre-Henry Soria. All Rights Reserved.
  6.  * @param string $sUrl
  7.  * @return string The favicon image.
  8.  */
  9. function get_favicon($sUrl)
  10. {
  11. $sApiUrl = 'http://www.google.com/s2/favicons?domain=';
  12. $sDomainName = get_domain($sUrl);
  13.  
  14. return $sApiUrl . $sDomainName;
  15. }
  16.  
  17. /**
  18.  * Get domain name from a URL (helper function).
  19.  *
  20.  * @author Pierre-Henry Soria <[email protected]>
  21.  * @copyright (c) 2013, Pierre-Henry Soria. All Rights Reserved.
  22.  * @param string $sUrl
  23.  * @return string $sUrl Returns the URL to lower case and without the www. if present in the URL.
  24.  */
  25. function get_domain($sUrl)
  26. {
  27. $sUrl = str_ireplace('www.', '', $sUrl);
  28. $sHost = parse_url($sUrl, PHP_URL_HOST);
  29. return $sHost;
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.