Как проверить, содержит ли страница ссылку?
January 26th, 2008 by mescalito
Иногда необходимо убедиться в том, что конкретной странице действительно есть конкретная ссылка.
PHP
-
function contains_link($page_url, $link_url) {
-
/* Get the page at page_url */
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_URL, $page_url);
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
-
-
curl_setopt($ch, CURLOPT_USERAGENT,
-
‘Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)’);
-
-
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
-
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
-
curl_setopt($ch, CURLOPT_FAILONERROR, true);
-
-
$html = curl_exec($ch);
-
curl_close($ch);
-
-
if(!$html) return false;
-
-
/* Remove HTML comments and their contents */
-
-
/* Extract all links */
-
$regexp=‘/(<a [\s]+[^>]*href\s*=\s*[\”\’]?)([^\’\” >]+)([\’\”]+[^<>]*>)/i’;
-
return false; /* No links on page */
-
};
-
-
/* Check each link */
-
foreach($matches as $match){
-
/* Skip links that contain rel=nofollow */
-
/* If URL = backlink_url, we’ve found the backlink */
-
if ($match[2]==$link_url) return true;
-
}
-
-
return false;
-
}
-
-
/* Usage example */
-
-
if (contains_link(‘http://w-shadow.com/’,‘http://w-shadow.com/blog/’)) {
-
echo ‘Reciprocal link found.’;
-
} else {
-
echo ‘Reciprocal link not found.’;
-
};
-
</a>
источник W-Shadow.com





















