<?php /* *下面的程序本人看到的外国的一个例子,我进行了翻译和加工 *Email:hcb0825@126.com */ /* 这个简单的例子要检查一个网页中的<a></a>中的链接段 */ // 要检查的网页中的链接 $url="http://michaelphipps.com"; // 被检查的网页页面 $link_page="http://curl.haxx.se/libcurl/php/examples/multi.html"; // 利用 Curl 返回网页会话资源 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$link_page); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result=curl_exec ($ch); curl_close ($ch); // Search for the $url on the $link_page. /* (下面的匹配模式有待完善,译者注) */ preg_match ("|<[aA] (.+?)".$url."(.+?)>(.+?)<//[aA]>|i", $result, $matches); // See if there were any matches /* 注意: preg_match 这个函数仅仅第一个匹配的, preg_match_all 这个函数可以返回所有能被匹配的链接) */ if (count($matches)>0){ // if there are items in the array, then there was a match. echo "The link exists on the target website"; print_r($matches); /* 如果需要一个比较特殊的链接文本,您需要改变查找的方式,而不是再匹配<a></a>标签了 If you require very specific link text you can use the information returned in the $matches array to further assess if your website is being linked the way you want. */ }else{ // if there are no items in the array, then no matches were found. echo "The link does not exist on the target website"; } ?>