-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_favicon.php
44 lines (33 loc) · 981 Bytes
/
get_favicon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
function get_favicon($url) {
// Load the HTML from the website
$html = file_get_contents("https://" . $url);
// Create a DOM object and parse the HTML
$dom = new DOMDocument();
@$dom->loadHTML($html);
$favicon = "";
// Find the link tag that contains the favicon
$links = $dom->getElementsByTagName('link');
foreach ($links as $link) {
if ($link->getAttribute('rel') == 'shortcut icon' ||
$link->getAttribute('rel') == 'icon') {
$favicon = $link->getAttribute('href');
break;
}
}
// If the favicon is a relative URL, prepend the website's base URL
if ($favicon[0] === "/") {
$favicon = "https://" . $url . '/' . ltrim($favicon, '/');
return $favicon;
}
return $favicon;
}
function get_title($url) {
$html = file_get_contents($url);
if (preg_match('/<title>(.*?)<\/title>/', $html, $matches)) {
$title = $matches[1];
} else {
$title = 'Title not found';
}
return $title;
}