当前位置:Gxlcms > PHP教程 > 请问个php正则表达式的用法,求高手帮忙

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

时间:2021-07-01 10:21:17 帮助过:6人阅读

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

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

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

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

HTML code
  1. <!--
  2. Code highlighting produced by Actipro CodeHighlighter (freeware)
  3. http://www.CodeHighlighter.com/
  4. -->csdn


HTML code
  1. <!--
  2. Code highlighting produced by Actipro CodeHighlighter (freeware)
  3. http://www.CodeHighlighter.com/
  4. -->csdn


HTML code
  1. <!--
  2. Code highlighting produced by Actipro CodeHighlighter (freeware)
  3. http://www.CodeHighlighter.com/
  4. -->csdn


HTML code
  1. <!--
  2. Code highlighting produced by Actipro CodeHighlighter (freeware)
  3. http://www.CodeHighlighter.com/
  4. -->csdn


HTML code
  1. <!--
  2. Code highlighting produced by Actipro CodeHighlighter (freeware)
  3. http://www.CodeHighlighter.com/
  4. -->csdn


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

PHP code
  1. <!--
  2. Code highlighting produced by Actipro CodeHighlighter (freeware)
  3. http://www.CodeHighlighter.com/
  4. -->
  5. $out=strtolower(@file_get_contents(http://www.xxx.com));
  6. if($out){
  7. $out=str_replace("\r\n","",$out);
  8. $out=str_replace("\r","",$out);
  9. $out=str_replace("\n","",$out);
  10. $havelink=preg_match_all('/(.*?)<\/a>/i', $out, $m);
  11. if($havelink||strstr($robots,'nofollow')){
  12. echo "友情链接存在";
  13. }
  14. }


求高手帮忙

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

PHP code
  1. $array = <<
  2. <div class="">
  3. <ul class="m-news-opt fix">
  4. <li class="opt-item">
  5. <a href="/PHPjiqiao-114236.html" target="_blank"><p>< 上一篇</p><p class="ellipsis">数组array_sum()如何求不了总和</p></a>
  6. </li>
  7. <li class="opt-item ta-r">
  8. <a href="/PHPjiqiao-114238.html" target="_blank"><p>下一篇 ></p><p class="ellipsis">phpusort排序有关问题</p></a>
  9. </li>
  10. </ul>
  11. </div>

人气教程排行