[PHP编程] php源码:自动识别文本中的链接

[复制链接]
发表于 2023-1-5 13:49:16
function text2links($str='') {
if(!preg_match('/(http|www\.|@)/i', $str)) { return $str; }
$lines = explode("<br />", $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:\/\/[^ )!]+)/i", "<a href=\"\\1\">\\1</a>", $l);

        $l = preg_replace("/(https:\/\/[^ )!]+)/i", "<a href=\"\\1\">\\1</a>", $l);

        $l = preg_replace("/(ftp:\/\/[^ )!]+)/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.'<br />';
}

return $new_text;
}

(function text2links($str='') {
if(!preg_match('/(http|www\.|@)/i', $str)) { return $str; }
$lines = explode("<br />", $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:\/\/[^ )!]+)/i", "<a href=\"\\1\">\\1</a>", $l) ;

$l = preg_replace("/(https:\/\/[^ )!]+)/i", "<a href=\"\\1\">\\1</a>", $l) ;

$l = preg_replace("/(ftp:\/\/[^ )!]+)/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.'<br />';
}

return $new_text;
})





上一篇:快乐挂扣1.2.1彩虹优化版(SAE可一键安装)
下一篇:最强在线网页源代码查看器

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表