Home  >  Article  >  Backend Development  >  熟悉正则表达式的大侠看过来

熟悉正则表达式的大侠看过来

WBOY
WBOYOriginal
2016-06-23 14:12:35932browse

小弟遇到点问题

现在从数据库读取一段字符串,使用正则表达式判断是否含有“http://”字符串
并且处理成标签可点击的

目前小弟纠结的是,如果连续的“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

//只要保证你的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的前面和连接的后面不一定为空


//只要保证你的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的前面和连接的后面不一定为空
举个例子,最好把你的特殊情况都列一下出来。

<?php$str = "http://dfhakoadghttp://daidgjiadpugidua";$reg = '#http://(?:\w(?!http://))+(?:\w(?=http://))?#'; // 其中\w的规则可以改成定义的规则$matches = array();preg_match_all($reg, $str, $mathces);print_r($mathces);



//只要保证你的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的前面和连接的后面不一定为空
举个例子,最好把你的特殊情况都列一下出来。
例如下面的一段内容:
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函数和正则表达式来把其中是链接的部分加上标签变成链接显示到网页上面,但是像ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.com这一句话比较不好匹配,只能匹配成一个链接标签

<?php$str = "http://dfhakoadghttp://daidgjiadpugidua";$reg = '#http://(?:\w(?!http://))+(?:\w(?=http://))?#'; // 其中\w的规则可以改成定义的规则$matches = array();preg_match_all($reg, $str, $mathces);print_r($mathces);

似乎理解一点了,我试一下

你这种用贪婪匹配应该可以
这里只考虑.com

$reg = '/(http:\/\/.*\.com)/isU';

<?php $str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.comhahahahttp://www.chinindfaasdl.com嗲话地哦862189375829http://www.chinindfaasdl.comhiaohdgioas862189375829http://www.chinindfaasdl.comhiaohdgioas撒旦法经理卡数据的分类http://www.chinindfaasdwfdfsdl.com";$str = preg_replace('/(http:\/\/.*?\.com)/', '<a href="\1">click</a>', $str);var_dump($str);



不知道你要换哪里? 我加了个click

<?php $str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.comhahahahttp://www.chinindfaasdl.com嗲话地哦862189375829http://www.chinindfaasdl.comhiaohdgioas862189375829http://www.chinindfaasdl.comhiaohdgioas撒旦法经理卡数据的分类http://www.chinindfaasdwfdfsdl.com";$str = preg_replace('/(http:\/\/.*?\.com)/', '<a href="\1">click</a>', $str);var_dump($str);



不知道你要换哪里? 我加了个click 感谢回答,不过只能筛选出.com的话不是我想达到的结果。。。
感觉http后面那一堆要筛选出除了“http://”以外的东西


<?php $str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.comhahahahttp://www.chinindfaasdl.com嗲话地哦862189375829http://www.chinindfaasdl.comhiaohdgioas862189375829http://www.chinindfaasdl.comhiaohdgioas撒旦法经理卡数据的分类http://www.chinindfaasdwfdfsdl.com";$str = preg_replace('/(http:\/\/.*?\.com)/', '<a href="\1">click</a>', $str);var_dump($str);



不知道你要换哪里? 我加了个click 感谢回答,不过只能筛选出.com的话不是我想达到的结果。。。
感觉http后面那一堆要筛选出除了“http://”以外的东西
目前应该不能完美的解决你这个问题。
因为http与下一个之间可能还有其它string,而这些又符合url规则,所以程序根本不能判断这string是属于上一个的url,还是两个url之间的间隔



<?php $str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.comhahahahttp://www.chinindfaasdl.com嗲话地哦862189375829http://www.chinindfaasdl.comhiaohdgioas862189375829http://www.chinindfaasdl.comhiaohdgioas撒旦法经理卡数据的分类http://www.chinindfaasdwfdfsdl.com";$str = preg_replace('/(http:\/\/.*?\.com)/', '<a href="\1">click</a>', $str);var_dump($str);



不知道你要换哪里? 我加了个click 感谢回答,不过只能筛选出.com的话不是我想达到的结果。。。
感觉http后面那一堆要筛选出除了“http://”以外的东西
目前应该不能完美的解决你这个问题。
因为http与下一个之间可能还有其它string,而这些又符合url规则,所以程序根本不能判断这string是属于上一个的url,还是两个url之间的间隔 我昨天试了一下QQ聊天里的识别,似乎也是吧http://后面的http识别成string了

$str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.comhahahahttp://www.chinindfaasdl.com嗲话地哦862189375829http://www.chinindfaasdl.comhiaohdgioas862189375829http://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);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn