-2

ドメインが私のサイトに公開される前に、(ユーザーが入力した)目的のURLをロードし、そのページが私のドメインにリンクしているかどうかを確認するスクリプトを作成しようとしています。私は正規表現の経験があまりなく、これが私がこれまでに持っているものです。

$loaded = file_get_contents('http://localhost/small_script/page.php');
// $loaded will be equal to the users site they have submitted
$current_site = 'site2.com';
// $current_site is the domain of my site, this the the URL that must be found in target site
$matches = Array();
$find = preg_match_all('/<a(.*?)href=[\'"](.*?)[\'"](.*?)\b[^>]*>(.*?)<\/a>/i', $loaded, $matches);

$c = count($matches[0]);
$z = 0;
while($z<$c){
  $full_link = $matches[0][$z];
  $href = $matches[2][$z];
  $z++;

  $check = strpos($href,$current_site);
    if($check === false) {

    }else{
    // The link cannot have the "no follow" tag, this is to check if it does and if so, return a specific error
    $pos = strpos($full_link,'no follow');

    if($pos === false) {
     echo $href;
    }
      else {
    //echo "rel=no follow FOUND";
    }

    }

}

ご覧のとおり、かなり乱雑で、どこに向かっているのか完全に確信しています。私は、誰かが私が試みたのとまったく同じことをする、小さくて速くて簡潔なスクリプトを私にくれることを望んでいました。

  1. ユーザーが入力したとおりに指定されたURLをロードします
  2. 指定されたURLが自分のサイトにリンクしているかどうかを確認します(そうでない場合は、エラーコード#1を返します)
  3. リンクがある場合は、「フォローなし」を確認してください。見つかった場合は、エラーコード#2を返します。
  4. すべて問題がない場合は、変数をtrueに設定して、他の関数(ページにリンクを表示するなど)を続行できるようにします。
4

1 に答える 1

0

これはコードです:) http://www.merchantos.com/makebeta/php/scraping-links-with-php/の助けを借りて

<?php

$my_url = 'http://online.bulsam.net';
$target_url = 'http://www.bulsam.net';
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';

// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
if (!$html) {
    echo "<br />cURL error number:" .curl_errno($ch);
    echo "<br />cURL error:" . curl_error($ch);
    exit;
}

// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);


// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

// find result
$result = is_my_link_there($hrefs, $my_url);

if ($result == 1) {

    echo 'There is no link!!!';
} elseif ($result == 2) {

    echo 'There is, but it is NO FOLLOW !!!';
} else {

    // blah blah blah
}

// used functions

function is_my_link_there($hrefs, $my_url) {

    for ($i = 0; $i < $hrefs->length; $i++) {

        $href = $hrefs->item($i);

        $url = $href->getAttribute('href');

        if ($my_url == $url) {

            $rel = $href->getAttribute('rel');

            if ($rel == 'nofollow') {

                return 2;
            }

            return 3;
        } 
    }

    return 1;
}
于 2010-09-24T01:29:30.093 に答える