脚本宝典收集整理的这篇文章主要介绍了php自动给网址加上链接的方法,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了PHP自动给网址加上链接的方法。分享给大家供大家参考。具体实现方法如下:
这里自动匹配页面里的网址,包含http,ftp等,自动给网址加上链接
<PRe class="brush:PHP;"> function text2links($str='') { if($str=='' or !preg_match('/(http|www\.|@)/i',$str)) { return $str; } $lines = explode("\n",$str); $new_text = ''; while (list($k,$l) = each($lines)) { // replace links: $l = preg_replace("/([ \t]|^)www\./i","\\1http://www.",$l); $l = preg_replace("/([ \t]|^)ftp\./i","\\1ftp://ftp.",$l); $l = preg_replace("/(http:\/\/[^ )\r\n!]+)/i","",$l); $l = preg_replace("/(https:\/\/[^ )\r\n!]+)/i",$l); $l = preg_replace("/(ftp:\/\/[^ )\r\n!]+)/i",$l); $l = preg_replace( "/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/i","",$l); $new_text .= $l."\n"; } return $new_text; }//使用范例:
$text = "Welcome www.js-code.COM :-)";
print text2links($text);
以上是脚本宝典为你收集整理的php自动给网址加上链接的方法全部内容,希望文章能够帮你解决php自动给网址加上链接的方法所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。