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;
}
}