簡體   English   中英

如何使用PHP從URL獲取域名?

[英]How to get domain name from URL with PHP?

我有2個表( url_feedclean_domains )。 我正在嘗試將url_feed所有數據url_feedclean_domains中,同時從url列插入domain

此外,它會在將行復制到url_feed后將statusqueued更改為completeclean_domains

這是2個表的樣子:

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

clean_domains

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 - 像這樣的網址將其域名輸出為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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM