簡體   English   中英

確保網址不重復

[英]Make sure url is not repeated

我正在建立一個網站,人們可以在其中提交其博客地址。 我想做的是,當他們提交博客時,讓我檢查數據庫以查看它是否已存在於數據庫中。

我的問題是有人可以將網址寫為“ http://blog.com”或“ http://www.blog.com”。

對我來說,檢查網址是否重復的最佳方法是什么?

我想是,我會檢查網址中是否包含“ http://”和“ www”,並檢查“ www”之后的部分,但是我覺得這很慢,因為我有3000多個網址。 謝謝!

www.blog.comblog.com 可能是也可能不是兩個完全不同的博客。 例如, example.blogspot.comblogspot.com是兩個完全不同的網站。 www. 只是一個普通的子域,與其他任何子域一樣,沒有規則。 域之后的路徑也是如此。 example.com/blorgexample.com/foobarg可能是兩個獨立的博客。

因此,您想向給定的URL發出HTTP請求,並查看它是否重定向到某處。 通常會有一個規范的URL,而www.blog.com重定向到blog.com或以其他方式重定向。 因此,請深入學習curl擴展或其他任何喜歡的HTTP請求模塊,以對給定的URL進行請求,並找出它解析為哪個規范的URL。

您可能還想使用parse_url解析整個URL,並且僅將例如主機名和路徑作為唯一標識符,而忽略諸如方案或查詢參數之類的其他不規則性。

我將創建一個實現一些比較接口(c#)的Url對象。

因此,您可以這樣做。

 var url = new Url("http://www.someblog.nl");
 var url2 = new Url("http://someblog.nl");

if (url == url2)
{
    throw new UrlNeedsToBeUniqueException();
}

您可以使用某些正則表達式來實現compare函數,也可以始終剝離www。 在開始比較之前,用字符串替換URL中的部分。

Dis-calmer:這是出於實驗目的,它旨在指導您選擇要使用的最佳格式

我認為您應該只保存域和子域..我將演示此簡單腳本的含義

圖像數組

$urls = array('http://blog.com',
        'http://somethingelse.blog.com',
        'http://something1.blog.com',
        'ftp://blog.com',
        'https://blog.com',
        'http://www.blog.com',
        'http://www.blog.net',
        'blog.com',
        'somethingelse.blog.com');

如果你跑

$found = array();
$blogUrl = new BlogURL();
foreach ( $urls as $url ) {
    $domain = $blogUrl->parse($url);
    if (! $domain) {
        $blogUrl->log("#Parse can't parse  $url");
        continue;
    }

    $key = array_search($domain, $found);

    if ($key !== false) {
        $blogUrl->log("#Duplicate $url same as {$found[$key]}");
        continue;
    }

    $found[] = $domain;
    $blogUrl->log("#new $url has  $domain");
}

var_dump($found);

輸出量

array
  0 => string 'blog.com' (length=8)
  1 => string 'somethingelse.blog.com' (length=22)
  2 => string 'something1.blog.com' (length=19)
  3 => string 'blog.net' (length=8)

如果您想了解內部工作

echo "<pre>";
echo implode(PHP_EOL, $blogUrl->getOutput());

輸出量

blog.com Found in http://blog.com
#new http://blog.com has  blog.com
somethingelse.blog.com Found in http://somethingelse.blog.com
#new http://somethingelse.blog.com has  somethingelse.blog.com
something1.blog.com Found in http://something1.blog.com
#new http://something1.blog.com has  something1.blog.com
#error domain not found in ftp://blog.com
#Parse can't parse  ftp://blog.com
blog.com Found in https://blog.com
#Duplicate https://blog.com same as blog.com
www.blog.com Found in http://www.blog.com
#Duplicate http://www.blog.com same as blog.com
www.blog.net Found in http://www.blog.net
#new http://www.blog.net has  blog.net
#Fixed blog.com to 
#Fixed http://blog.com to http://blog.com
blog.com Found in http://blog.com
#Duplicate blog.com same as blog.com
#Fixed somethingelse.blog.com to 
#Fixed http://somethingelse.blog.com to http://somethingelse.blog.com
somethingelse.blog.com Found in http://somethingelse.blog.com
#Duplicate somethingelse.blog.com same as somethingelse.blog.com

使用的類

class BlogURL {
    private $output;

    function parse($url) {
        if (! preg_match("~^(?:f|ht)tps?://~i", $url)) {
            $this->log("#Fixed $url to ");
            $url = "http://" . $url;
            $this->log("#Fixed $url to $url");
        }

        if (! filter_var($url, FILTER_VALIDATE_URL)) {
            $this->log("#Error $url not valid");
            return false;
        }
        preg_match('!https?://(\S+)+!', $url, $matches);
        $domain = isset($matches[1]) ? $matches[1] : null;

        if (! $domain) {
            $this->log("#error domain not found in $url");
            return false;
        }
        $this->log($domain . " Found in $url");

        return ltrim($domain, "w.");
    }

    function log($var = PHP_EOL) {
        $this->output[] = $var;
    }

    function getOutput() {
        return $this->output;
    }
}

暫無
暫無

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

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