
WEBサイトの内部外部リンクを調べる為に、スクレイピングライブラリ「Simple HTML DOM Parser」を使用して内外部リンクを調べる関数を作成しました。
Simple HTML DOM Parserのダウンロードはこちら↓
Download PHP Simple HTML DOM Parser from SourceForge.net
A php based DOM parser.
論よりコード
<?php
function linkAnalyser($my_url) {
// 実行時間を設定します。
ini_set('max_execution_time', 20*60);
// Simple HTML DOM Parser を読み込みます。
require_once ("simple_html_dom.php");
$ex_data = $ex_data_arr = $int_data = $ext_data = array();
$t_count = 0;
$i_links = 0;
$e_links = 0;
$i_nofollow = 0;
$e_nofollow = 0;
// URLからデータを取得します。
$data = file_get_html($my_url);
if($data == '')
return false;
// URLを解析します。
$my_url_parse = parse_url($my_url);
$my_url_host = str_replace("www.", "", $my_url_parse['host']);
$find_out = $data->find("a");
// リンクを抽出します。
foreach ($find_out as $href) {
if (!in_array($href->href, $ex_data_arr)) {
if (substr($href->href, 0, 1) != "" && $href->href != "#") {
$ex_data_arr[] = $href->href;
$ex_data[] = array(
'href' => $href->href,
'rel' => $href->rel
);
}
}
}
// 内部リンクをチェックします。
foreach ($ex_data as $link) {
$t_count++;
$parse_urls = parse_url($link['href']);
$type = strtolower($link['rel']);
if ($parse_urls['host'] == $my_url_host || $parse_urls['host'] == "www." . $my_url_host) {
$i_links++;
$int_data[$i_links]['inorout'] = "internal";
$int_data[$i_links]['href'] = $link['href'];
if ($type == 'dofollow' || ($type != 'dofollow' && $type != 'nofollow')) {
$int_data[$i_links]['follow_type'] = "dofollow";
}
if ($type == 'nofollow') {
$i_nofollow++;
$int_data[$i_links]['follow_type'] = "nofollow";
}
}
elseif ((substr($link['href'], 0, 2) != "//") && (substr($link['href'], 0, 1) ==
"/")) {
$i_links++;
$int_data[$i_links]['inorout'] = "internal";
$int_data[$i_links]['href'] = $link['href'];
if ($type == 'dofollow' || ($type != 'dofollow' && $type != 'nofollow')) {
$int_data[$i_links]['follow_type'] = "dofollow";
}
if ($type == 'nofollow') {
$i_nofollow++;
$int_data[$i_links]['follow_type'] = "nofollow";
}
}
}
// 外部リンクをチェックします。
foreach ($ex_data as $link) {
$parse_urls = parse_url($link['href']);
$type = strtolower($link['rel']);
if ($parse_urls !== false && isset($parse_urls['host']) && $parse_urls['host'] !=
$my_url_host && $parse_urls['host'] != "www." . $my_url_host) {
$e_links++;
$ext_data[$e_links]['inorout'] = "external";
$ext_data[$e_links]['href'] = $link['href'];
if ($type == 'dofollow' || ($type != 'dofollow' && $type != 'nofollow')) {
$ext_data[$e_links]['follow_type'] = "dofollow";
}
if ($type == 'nofollow') {
$e_nofollow++;
$ext_data[$e_links]['follow_type'] = "nofollow";
}
}
elseif ((substr($link['href'], 0, 2) == "//") && (substr($link['href'], 0, 1) !=
"/")) {
$e_links++;
$ext_data[$e_links]['inorout'] = "external";
$ext_data[$e_links]['href'] = $link['href'];
if ($type == 'dofollow' || ($type != 'dofollow' && $type != 'nofollow')) {
$ext_data[$e_links]['follow_type'] = "dofollow";
}
if ($type == 'nofollow') {
$e_nofollow++;
$ext_data[$e_links]['follow_type'] = "nofollow";
}
}
}
return array(
$int_data,
$i_links,
$i_nofollow,
$ext_data,
$e_links,
$e_nofollow,
$t_count
);
}
?>
ポイント
dofollowかnofollowかも分かるようになってます。
内外部リンクチェックを使ってみる
上記関数を実装したページです。動作を確認する場合はこちらを使ってみてください。
内外部リンクチェック | SEOLIG
ブラウザ上でリンク状態を調査する簡単操作で登録不要無料のツール。指定されたURLの内部リンク、外部リンクを検査します。リンク数、内部外部リンクURL、nofollowまたはdofollowを一覧に表示します。CSVダウンロード可。