http地址自动检测并添加URL链接
javascript版本
1 String.prototype.html = function(){
2 var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
3 return this.replace(reg, '<a href="$1$2">$1$2</a>');
4 };
5
6 var v = "欢迎访问我的博客:http://www.cnblogs.com/yimu";
2 var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
3 return this.replace(reg, '<a href="$1$2">$1$2</a>');
4 };
5
6 var v = "欢迎访问我的博客:http://www.cnblogs.com/yimu";
7 alert(v.html()); //欢迎访问我的博客:<a href="http://www.cnblogs.com/yimu">http://www.cnblogs.com/yimu</a>
PHP版本
1 <?php
2
3 $str2 = 'http://www.cnblogs.com/yimu其他字符http://www.cnblogs.com/yimu';
4 $pattern2 = "/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/i";
5 print preg_replace($pattern2, '<a href="\\1\\2">\\1\\2</a>', $str2);
6 ?>
2
3 $str2 = 'http://www.cnblogs.com/yimu其他字符http://www.cnblogs.com/yimu';
4 $pattern2 = "/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/i";
5 print preg_replace($pattern2, '<a href="\\1\\2">\\1\\2</a>', $str2);
6 ?>
输出:
<a href="http://www.cnblogs.com/yimu">http://www.cnblogs.com/yimu</a>其他字符 <a href="http://www.cnblogs.com/yimu">http://www.cnblogs.com/yimu</a> | |