如何使用PHP从URL中获取域名


How to get domain name from URL with PHP?

我有两个表(url_feedclean_domains)。我试图将url_feed中的所有数据复制到clean_domains中,同时插入url列中的domain

此外,在将行复制到clean_domains之后,它将在url_feed中将statusqueued更改为complete

下面是这两张表的样子:

url_feed

id | url                                    | matches | networks                    | status
1  | http://whatever.com/example1.php       | 5       | Facebook::Twitter Inc       | queued
2  | http://www.example.com/other-stuff.php | 2       | MySpace::Facebook::Twitter  | queued
3  | http://www.test.com/random-text        | 12      | Instagram::Twitter          | queued

清理域

id | domain       | url                                     | matches | networks                    | status
1  | whatever.com | http://whatever.com/example1.php        | 5       | Facebook::Twitter Inc       | queued
2  | example.com  | http://www.example.com/other-stuff.php  | 2       | MySpace::Facebook::Twitter  | queued
3  | test.com     | http://www.test.com/random-text         | 12      | Instagram::Twitter          | queued

这是我的代码:

<?php
$con=mysqli_connect("localhost","redacted","redacted","redacted");
mysqli_query($con,"INSERT INTO clean_domains
(id,domain,url,matches,networks)
  SELECT
    id,
    SUBSTRING_INDEX(REPLACE(REPLACE(REPLACE(REPLACE(url, 'http://', ''), 'https://', ''), 'http://www.', ''), 'https://www.', ''), '/', 1),
    url,
    matches,
    networks
  FROM url_feed
  WHERE status = 'queued'");
mysqli_query($con,"UPDATE url_feed
SET    status = 'complete'
WHERE status = 'queued' AND
id IN (SELECT id
FROM   clean_domains)");
mysqli_close($con);
?>

我的代码适用于99%的域,但我不太清楚如何使其完美工作。

以下是它似乎不完美工作的3次:

  1. 像这样的冒号-http://example.com:88/testing-URL的域输出为example.com:88,而我希望它是example.com

  2. IP地址-http://188.123.44.12/test.php-对于IP,它似乎正确地将IP地址输入到数据库中。在本例中,它将输入188.123.44.12作为domain,但我不希望这样。我只想要域名,所以如果它是一个IP,就不应该被复制。它应该在url_feed中标记为complete,然后移到下一行。

  3. 子域-http://subdomain.whatever.example.com/test.html-当我希望它为example.com时,它将作为subdomain.whatever.example.com输入到domain列中。

我唯一能想到的验证输入的域是否真的是域的方法是对每个域运行whois查询。如果它不是有效的,它会删除第一块文本。例如,它不会得到subdomain.whatever.example.com的有效结果,所以它尝试whatever.example.com,然后尝试example.com,直到结果有效,或者它跳过它并将status列标记为complete

有什么想法可以改变以使其正常工作吗?

这就是我现在的处境:

$_url_string = 'https://testfewfew.dsd.google.co.uk/testing/whatever';
preg_match("/[a-z0-9'-]{1,63}'.[a-z'.]{2,6}$/", parse_url($_url_string, PHP_URL_HOST), $_domain_tld);
echo $_domain_tld[0];

只需使用内置的php函数parse_url

您可以从类似的主机名中筛选子域

$url = 'http://subdomain.whatever.example.com/test.html';
$data = parse_url($url);
$host = $data['host'];
$hostname = explode(".", $host);
$domain = $hostname[count($hostname)-2] . "." . $hostname[count($hostname)-1];
print $domain;

将输出

example.com

如果你有一个带端口的urlparse_url会很容易处理它,例如

$url = 'http://example.com:88/testing';
$data = parse_url($url);
print_r($data);

将输出

Array
(
    [scheme] => http
    [host] => example.com
    [port] => 88
    [path] => /testing
)

下面您将检查主机名是否为有效的IP地址或

$url = 'http://188.123.44.12/test.php';
$data = parse_url($url);
print_r($data);
$hostIsIpAddress = ip2long($data['host']) !== false;
var_dump($hostIsIpAddress);

将分别输出bool(true)bool(false)