php获取字符串中url自动加超级链接
有时我们希望用户提交的连接地址自动给这个连接加上a标签了,这个其实就是给文本url地址加超级链接了,下面整理了几个例子希望对各位会带来帮助.
PHP实例代码如下:
$string = "这是php提取文本内容字符串中的网址,连接为http://HdhCmsTestphpfensi测试数据 站点" ; //连接后需要有个空格或回车。 $string = eregi_replace ( "http://([^ ,\r\n]*)" , "<a href=\\0 target=_blank>\\0</a>" , $string ); $string = eregi_replace ( "ftp://([^ ,\r\n]*)" , "<a href=\\0 target=_blank>\\0</a>" , $string ); 例子1 代码如下: <?php $str = '这样的http://HdhCmsTestphpfensi测试数据/ play/?id=1010&p=1_0-1试试!' ; $pattern = '/(http:\/\/|https:\/\/|ftp:\/\/)([\w:\/\.\?=&-_]+)/is' ; $str =preg_replace( $pattern , '<a href=\1\2>\2</a>' , $str ); echo $str ; ?> 例子2 代码如下: <?php function autolink( $foo ) { $foo = eregi_replace ( '(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)' , '<a href="/1" mce_href="/1" target=_blank rel=nofollow>/1</a>' , $foo ); if ( strpos ( $foo , "http" ) === FALSE ){ $foo = eregi_replace ( '(HdhCmsTest[-a-zA-Z0-9@:%_/+.~#?&//=]+)' , '<a href="http:///1" mce_href="http:///1" target=_blank rel=nofollow >/1</a>' , $foo ); } else { $foo = eregi_replace ( '([[:space:]()[{}])(HdhCmsTest[-a-zA-Z0-9@:%_/+.~#?&//=]+)' , '/1<a href="http:///2" mce_href="http:///2" target=_blank rel=nofollow >/2</a>' , $foo ); } return $foo ; } ?>查看更多关于php获取字符串中url自动加超级链接 - php高级应用的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did29824