请教个php正则表达式的用法,求高手帮忙
想做个友情链接的检测方法,检测对方网站是不是有我的网站的友情链接,也就是判断是否有我网站的地址;
例如:我的网站的地址是www.csdn.net,
但是有些网站有时候会在链接里面加个rel="nofollow",这样友情链接就没有意义了;
正则没好好学,问下怎么才能检测出是否加了这个呢?
有好几种情况,例如:
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><a rel="nofollow" href="http://www.csdn.net">csdn</a>
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><a target="_blank" rel="nofollow" href="http://www.csdn.net">csdn</a>
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><a target="_blank" rel="nofollow" href="http://www.csdn.net">csdn</a>
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><a rel="nofollow" href="http://www.csdn.net" target="_blank">csdn</a>
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><a href="http://www.csdn.net" target="_blank" rel="nofollow">csdn</a>
<!--
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(.*?)href=(.*?)http://www.csdn.net(.*?)>(.*?)<\/a>/i', $out, $m);
if($havelink||strstr($robots,'nofollow')){
echo "友情链接存在";
}
}
$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.>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号