自动识别内容中的Url并添加超链接
2011-08-25 17:46
1241 查看
<?php
$str ='<p>m.ipal.com. m.ipal.com,m.ipal.com;m.ipal.com/?task=u&se;test auto link www.baidu.com staff.maosong.com m.ipal xiaonei.com m.ipal.com www.ipal.com/?_task=mail#a http://www.ipal.com http://www.policecard.com/?_task=mail http://www.ipal.com/?_task=index#bb</p>';
echo autolink($str);
function autolink($str){
$body = preg_replace(array("/(?<=[^a-z0-9\"'=\/\-_\.~\?]|^)((https?|ftp|gopher|news|telnet|mms){1}:\/\/)?(([a-z]([a-z0-9\-])*(\.[a-z]([a-z0-9\-])*)+(?=\.(\s|$)))|((([a-z]([a-z0-9\-])*\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?(&|[a-z0-9+_\-\.%=&])*)?)?(#[a-z][a-z0-9_]*)?((?=$|[,\s<]|\.\s)|(?!p)(?=;))))/ies"
),
array("urlcut('\$1\$3', '\$2')"
),
$str);
return $body;
}
function urlcut($url, $prefix)
{
$urllink = "<a href=\"" . (empty($prefix) ? "http://$url" : $url) . "\" target=\"_blank\">";
$urllink .= "$url</a>";
return $urllink;
}
?>
$str ='<p>m.ipal.com. m.ipal.com,m.ipal.com;m.ipal.com/?task=u&se;test auto link www.baidu.com staff.maosong.com m.ipal xiaonei.com m.ipal.com www.ipal.com/?_task=mail#a http://www.ipal.com http://www.policecard.com/?_task=mail http://www.ipal.com/?_task=index#bb</p>';
echo autolink($str);
function autolink($str){
$body = preg_replace(array("/(?<=[^a-z0-9\"'=\/\-_\.~\?]|^)((https?|ftp|gopher|news|telnet|mms){1}:\/\/)?(([a-z]([a-z0-9\-])*(\.[a-z]([a-z0-9\-])*)+(?=\.(\s|$)))|((([a-z]([a-z0-9\-])*\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?(&|[a-z0-9+_\-\.%=&])*)?)?(#[a-z][a-z0-9_]*)?((?=$|[,\s<]|\.\s)|(?!p)(?=;))))/ies"
),
array("urlcut('\$1\$3', '\$2')"
),
$str);
return $body;
}
function urlcut($url, $prefix)
{
$urllink = "<a href=\"" . (empty($prefix) ? "http://$url" : $url) . "\" target=\"_blank\">";
$urllink .= "$url</a>";
return $urllink;
}
?>
相关文章推荐
- ASP.NET自动给内容中的URL加上超链接(中国万网)
- wpf,能够复制文字 及自动识别URL超链接的TextBlock
- AndroidStudio使用注释模版使创建文件时自动添加默认的内容
- 在ASP.NET中自动给URL地址加上超链接在ASP.NET中自动给URL地址加上超链接
- JTextArea添加的内容自动换行
- 在ASP.NET中自动给URL加上超链接(问题篇)
- php自动转换URL,跳转至超链接
- 在ASP.NET中自动给URL地址加上超链接
- 在ASP.NET中自动给URL地址加上超链接
- URL参数GB2312和UTF-8编码 自动识别
- 四二、js操作DOM(dom)改变css3属性时封装方法使自动识别浏览器添加css3属性前缀
- JavaScript实现url地址自动检测并添加URL链接
- 自动识别OpenCV版本并添加依赖库
- 复制网站文章内容时自动添加版权信息的JS代码
- JavaFX表格控件TableView高级应用:自动添加ID列、删除操作列、单元格内容个性化渲染
- 利用Django中的url方法实现地址动态拼接自动生成超链接地址
- .net 导出excel方法,内容自动识别科学计数法去除方式style= 'vnd.ms-excel.numberformat:@ '
- ASP.NET自动给URL加上超链接
- iOS UITextView内容添加后,自动滚动到最后一行
- 在ASP.NET中自动给URL地址加上超链接