给文本中的url加超级链接,同时滤过已加过链接的url
/**
* 给文本中的url加超级链接,同时滤过已有链接的url
* @param string $str [description]
* @return [type] [description]
*/
function text2links($str='') {
if($str=='' or !preg_match('/(http|www\.|@)/i', $str)) return $str; $lines = explode("\n", $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."\n";
}
return $new_text;
}
$text = "open: http://www.baidu.com/ and <a href='http://www.baidu.com'>hehe</a>";
var_dump(text2links($text));
输出结果:
string(109) "open:<a href=" http://www.baidu.com/"> http://www.baidu.com/</a> and <a href='http://www.baidu.com'>hehe</a>"
给文本中的url加超级链接,同时滤过已加过链接的url的更多相关文章
- javascript异步延时加载及判断是否已加载js/css文件
<html> <head> <script type="text/javascript"> /**======================= ...
- PHP实现把文本中的URL转换为链接的auolink()
转载:http://www.jb51.net/article/52916.htm 其实我在<把文本中的URL地址转换为可点击链接的JavaScript.PHP自定义函数>一文中介绍过PHP ...
- js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可)
js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可) 一.总结 ajax读取json和读取普通文本,和获 ...
- 在文本中匹配链接并添加A标签
(?<!href="|">)(https?:\/\/[\w\-\.!~?&=+\*\'(),\/]+)((?!\<\/\a\>).)* 这个正则可以 ...
- IOS 正则表达式匹配文本中URL位置并获取URL所在位置(解决连接中文问题)
需求很简单,是从一段文本中匹配出其中的超链接.基本的做法就是用正则表达式去匹配.但是有这样一个问题. 网上大部分的识别URL的正则表达式url末尾有空格的情况下可以正确识别.比如这样的情况. 我是一段 ...
- iOS 检测文本中的 URL、电话号码等信息
iOS 检测文本中的 URL.电话号码等信息 要检测文本中的 URL.电话号码等,除了用正则表达式,还可以用 NSDataDetector. 用 NSTextCheckingResult.Checki ...
- phpExcel常用方法详解【附有php导出excel加超级链接】
phpExcel常用方法详解[附有php导出excel加超级链接] 发表于4年前(-- :) 阅读() | 评论() 0人收藏此文章, 我要收藏 赞0 http://www.codeplex.com/ ...
- PHP正则表达式-从文本中提取URL
1.从文本中提取URL的正则表达式 '/https?:\/\/[\w-.%#?\/\\\]+/i'
- 如何使用JS脚本从HTML中分离图片标签与文本,替换文本中指定的内容并加粗(原创)
var html='ddfsdfsdfdsd dfsdfsdffds<img _src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4 ...
随机推荐
- numpy 加速 以及 ipython
先安装openblas, 然后用pip 安装numpy sudo ln -s /usr/lib64/libopenblas-r0.2.14.so /usr/lib64/libopenblas.so 为 ...
- [Algo] 625. Longest subarray contains only 1s
Given an array of integers that contains only 0s and 1s and a positive integer k, you can flip at mo ...
- 吴裕雄--天生自然 PYTHON3开发学习:条件控制
if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3 var1 = ...
- layer日期控件,开始时间不能大于结束时间
var start=laydate.render({ elem: '#beginTime', //指定元素 type: 'month', format:'yyyy-MM', done:function ...
- 三十、sersync高级同步工具实时数据同步架构
一.项目介绍 Sersync项目利用inotity与rsync技术实现对服务器数据实时同步的解决方案,其中inotity用于监控sersync所在服务器上的文件变化. Sersync项目的优点: 1. ...
- 50)PHP,单例模式
class B{ ; private static $instance; private function __construct(){} public static function getNew( ...
- springboot学习笔记:12.解决springboot打成可执行jar在linux上启动慢的问题
有时候,当你把你的springboot项目打成可执行的jar,放在linux上启动时,发现启动超级慢: 这往往是因为springboot内置tomcat启动时实例化SecureRandom对象随机数策 ...
- php获取mysql大小
查看指定数据库大小: SELECT sum(DATA_LENGTH)+sum(INDEX_LENGTH) FROM information_schema.TABLES where TABL ...
- 100)PHP,文件上传总代码整理
首先是我的目录关系: 然后我的html表单代码: <html> <head> <title>Form</title> <meta http-equ ...
- SpringBoot之HandlerInterceptor拦截器的使用 ——(三)获取requestBody解决java.io.IOException: Stream closed
原文地址:https://blog.csdn.net/zhibo_lv/article/details/81875705 感谢原作者