>백엔드 개발 >PHP 튜토리얼 > 请问个php正则表达式的用法,求高手帮忙

请问个php正则表达式的用法,求高手帮忙

WBOY
WBOY원래의
2016-06-13 13:23:011093검색

请教个php正则表达式的用法,求高手帮忙
想做个友情链接的检测方法,检测对方网站是不是有我的网站的友情链接,也就是判断是否有我网站的地址;

例如:我的网站的地址是www.csdn.net,

但是有些网站有时候会在链接里面加个rel="nofollow",这样友情链接就没有意义了;

正则没好好学,问下怎么才能检测出是否加了这个呢?
有好几种情况,例如:

HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><a rel="nofollow" href="http://www.csdn.net">csdn</a>


HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><a target="_blank" rel="nofollow" href="http://www.csdn.net">csdn</a>


HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><a target="_blank" rel="nofollow" href="http://www.csdn.net">csdn</a>


HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><a rel="nofollow" href="http://www.csdn.net" target="_blank">csdn</a>


HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><a href="http://www.csdn.net" target="_blank" rel="nofollow">csdn</a>


以下是我在网上找的别人检测地址的代码:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$out=strtolower(@file_get_contents(http://www.xxx.com));
      if($out){
          $out=str_replace("\r\n","",$out);
          $out=str_replace("\r","",$out);
          $out=str_replace("\n","",$out);
          $havelink=preg_match_all('/<a>(.*?)/i', $out, $m);
           
          if($havelink||strstr($robots,'nofollow')){
                         echo "友情链接存在";

}
                   }


</a>


求高手帮忙

------解决方案--------------------
迂回一下,正则我也很弱,找出和自己相关的链接,然后判断是否存在关键代码

PHP code

$array = csdn
<a target="_blank" rel="nofollow" href="http://www.csdn.net">csdn</a>
<a target="_blank" rel="nofollow" href="http://www.csdn.net">csdn</a>
<a rel="nofollow" href="http://www.csdn.net" target="_blank">csdn</a>
HTML;
preg_match_all('/(<a.>)/is', $array, $match);
if (isset($match[1])) {
    foreach ($match[1] as $html) {
        if (stripos($html, 'rel="nofollow"') !== FALSE)
            echo 'bad!';
        break;
    }

} <div class="clear">
                 
              
              
        
            </div></a.>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.