小弟遇到点问题现在从数据库读取一段字符串,使用正则表达式判断是否含有“http://”字符串
并且处理成<a>标签可点击的目前小弟纠结的是,如果连续的“http://”挨在一起正则就只能判断为一个连接了,不知道该怎么处理了-——水平有限啊例如“http://dfhakoadghttp://daidgjiadpugidua”识别成http://dfhakoadg和http://daidgjiadpugidua
并且处理成<a>标签可点击的目前小弟纠结的是,如果连续的“http://”挨在一起正则就只能判断为一个连接了,不知道该怎么处理了-——水平有限啊例如“http://dfhakoadghttp://daidgjiadpugidua”识别成http://dfhakoadg和http://daidgjiadpugidua
//只要保证你的url含有http开头的话,可以不使用正则:
$s = 'http://dfhakoadghttp://daidgjiadpugidua';
$a = explode('http://', $s);
foreach($a as $r) {
if($r) echo "http://$r", '<br/>';
}
//http://dfhakoadg
//http://daidgjiadpugidua
问题是类似于QQ聊天那种的,http的前面和连接的后面不一定为空
问题是类似于QQ聊天那种的,http的前面和连接的后面不一定为空
举个例子,最好把你的特殊情况都列一下出来。
<?php$str = "http://dfhakoadghttp://daidgjiadpugidua";$reg = '#http://(?:\w(?!http://))+(?:\w(?=http://))?#'; // 其中\w的规则可以改成定义的规则$matches = array();preg_match_all($reg, $str, $mathces);print_r($mathces);
问题是类似于QQ聊天那种的,http的前面和连接的后面不一定为空
举个例子,最好把你的特殊情况都列一下出来。
例如下面的一段内容:
ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.com
hahahahttp://www.chinindfaasdl.com嗲话地哦862189375829http://www.chinindfaasdl.comhiaohdgioas
862189375829http://www.chinindfaasdl.comhiaohdgioas
撒旦法经理卡数据的分类http://www.chinindfaasdwfdfsdl.com
整个一大段内容放到一个变量$ret里面,然后使用preg_replace函数和正则表达式来把其中是链接的部分加上<a>标签变成链接显示到网页上面,但是像ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.com这一句话比较不好匹配,只能匹配成一个链接标签
这里只考虑.com$reg = '/(http:\/\/.*\.com)/isU';
$str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.com
hahahahttp://www.chinindfaasdl.com嗲话地哦862189375829http://www.chinindfaasdl.comhiaohdgioas
862189375829http://www.chinindfaasdl.comhiaohdgioas
撒旦法经理卡数据的分类http://www.chinindfaasdwfdfsdl.com";
$str = preg_replace('/(http:\/\/.*?\.com)/', '<a href="\1">click</a>', $str);
var_dump($str);
不知道你要换哪里? 我加了个click
感觉http后面那一堆要筛选出除了“http://”以外的东西
因为http与下一个之间可能还有其它string,而这些又符合url规则,所以程序根本不能判断这string是属于上一个的url,还是两个url之间的间隔
因为http与下一个之间可能还有其它string,而这些又符合url规则,所以程序根本不能判断这string是属于上一个的url,还是两个url之间的间隔我昨天试了一下QQ聊天里的识别,似乎也是吧http://后面的http识别成string了
hahahahttp://www.chinindfaasdl.com嗲话地哦862189375829http://www.chinindfaasdl.comhiaohdgioas
862189375829http://www.chinindfaasdl.comhiaohdgioas
撒旦法经理卡数据的分类http://www.chinindfaasdwfdfsdl.com";
echo preg_replace('#http://[\w\.-]+\.(com|net|org|hk|info|cc|edu)#s', '<a href="$0">$0</a>', $str);