[英]Make sure url is not repeated
我正在建立一个网站,人们可以在其中提交其博客地址。 我想做的是,当他们提交博客时,让我检查数据库以查看它是否已存在于数据库中。
我的问题是有人可以将网址写为“ http://blog.com”或“ http://www.blog.com”。
对我来说,检查网址是否重复的最佳方法是什么?
我想是,我会检查网址中是否包含“ http://”和“ www”,并检查“ www”之后的部分,但是我觉得这很慢,因为我有3000多个网址。 谢谢!
www.blog.com
和blog.com
可能是也可能不是两个完全不同的博客。 例如, example.blogspot.com
和blogspot.com
是两个完全不同的网站。 www.
只是一个普通的子域,与其他任何子域一样,没有规则。 域之后的路径也是如此。 example.com/blorg
和example.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.