php 自动给网址添加超链接的功能实例
内容摘要
这篇文章主要为大家详细介绍了php 自动给网址添加超链接的功能实例,具有一定的参考价值,可以用来参考一下。
自动匹配页面里的网址,包含http,ftp等,自动给网址加上链接,php自动给
自动匹配页面里的网址,包含http,ftp等,自动给网址加上链接,php自动给
文章正文
这篇文章主要为大家详细介绍了php 自动给网址添加超链接的功能实例,具有一定的参考价值,可以用来参考一下。
自动匹配页面里的网址,包含http,ftp等,自动给网址加上链接,php自动给网址加上超链接,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <code class = "php" > /** * 自动匹配页面里的网址,包含http,ftp等,自动给网址加上链接 * * @param * @arrange (www.idcnote.com) **/ 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" , "<a href=\"\\1\">\\1</a>" , $l ); $l = preg_replace( "/(https:\/\/[^ )\r\n!]+)/i" , "<a href=\"\\1\">\\1</a>" , $l ); $l = preg_replace( "/(ftp:\/\/[^ )\r\n!]+)/i" , "<a href=\"\\1\">\\1</a>" , $l ); $l = preg_replace( "/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/i" , "<a href=\"mailto:\\1\">\\1</a>" , $l ); $new_text .= $l . "\n" ; } return $new_text ; } //使用范例: $text = "Visit www.idcnote.com :-)" ; print text2links( $text ); /*** 来自php教程(www.idcnote.com) ***/ </code> |
注:关于php 自动给网址添加超链接的功能实例的内容就先介绍到这里,更多相关文章的可以留意
代码注释