11
08月
2015
11. 转换 URL:从字符串变成超链接
如果你正在开发论坛,博客或者是一个常规的表单提交,很多时候都要用户访问一个网站。使用这个函数,URL 字符串就可以自动的转换为超链接。
1
2
3
4
5
6
7
8
9
10
11
|
function makeClickableLinks( $text )
{
$text = eregi_replace ( '(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)' ,
'<a href="\1">\1</a>' , $text );
$text = eregi_replace ( '([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)' ,
'\1<a href="http://\2">\2</a>' , $text );
$text = eregi_replace ( '([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})' ,
'<a href="mailto:\1">\1</a>' , $text );
return $text ;
}
|
语法:
1
2
3
4
5
|
<?php
$text = "This is my first post on http://blog.koonk.com" ;
$text = makeClickableLinks( $text );
echo $text ;
?>
|
特殊说明,本文版权归 ning个人博客 所有带原创标签请勿转载,转载请注明出处.