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

shuiguoqu 2013-06-07 09:06:43
小弟遇到点问题

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

目前小弟纠结的是,如果连续的“http://”挨在一起正则就只能判断为一个连接了,不知道该怎么处理了-——

水平有限啊。。。

例如“http://dfhakoadghttp://daidgjiadpugidua”

识别成http://dfhakoadg和http://daidgjiadpugidua
...全文
215 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
床上等您 2013-06-08
  • 打赏
  • 举报
回复
引用 9 楼 shuiguoqu 的回复:
[quote=引用 8 楼 hellodifa 的回复:]
<?php 
$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
感谢回答,不过只能筛选出.com的话不是我想达到的结果。。。 感觉http后面那一堆要筛选出除了“http://”以外的东西[/quote] 目前应该不能完美的解决你这个问题。 因为http与下一个之间可能还有其它string,而这些又符合url规则,所以程序根本不能判断这string是属于上一个的url,还是两个url之间的间隔
shuiguoqu 2013-06-08
  • 打赏
  • 举报
回复
引用 8 楼 hellodifa 的回复:
<?php 
$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
感谢回答,不过只能筛选出.com的话不是我想达到的结果。。。 感觉http后面那一堆要筛选出除了“http://”以外的东西
一起混吧 2013-06-08
  • 打赏
  • 举报
回复
$str="ahahttp://www.chinindfaasdl.comahahttp://www.chinindfaasdl.com
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);
shuiguoqu 2013-06-08
  • 打赏
  • 举报
回复
引用 10 楼 yangball 的回复:
[quote=引用 9 楼 shuiguoqu 的回复:] [quote=引用 8 楼 hellodifa 的回复:]
<?php 
$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
感谢回答,不过只能筛选出.com的话不是我想达到的结果。。。 感觉http后面那一堆要筛选出除了“http://”以外的东西[/quote] 目前应该不能完美的解决你这个问题。 因为http与下一个之间可能还有其它string,而这些又符合url规则,所以程序根本不能判断这string是属于上一个的url,还是两个url之间的间隔[/quote]我昨天试了一下QQ聊天里的识别,似乎也是吧http://后面的http识别成string了
ywq0127 2013-06-07
  • 打赏
  • 举报
回复
你这种用贪婪匹配应该可以 这里只考虑.com

$reg = '/(http:\/\/.*\.com)/isU';
shuiguoqu 2013-06-07
  • 打赏
  • 举报
回复
引用 4 楼 hnxxwyq 的回复:

<?php

$str = "http://dfhakoadghttp://daidgjiadpugidua";

$reg = '#http://(?:\w(?!http://))+(?:\w(?=http://))?#'; // 其中\w的规则可以改成定义的规则

$matches = array();

preg_match_all($reg, $str, $mathces);

print_r($mathces);
似乎理解一点了,我试一下
shuiguoqu 2013-06-07
  • 打赏
  • 举报
回复
引用 3 楼 yangball 的回复:
[quote=引用 2 楼 shuiguoqu 的回复:] [quote=引用 1 楼 yangball 的回复:]

//只要保证你的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的前面和连接的后面不一定为空[/quote] 举个例子,最好把你的特殊情况都列一下出来。[/quote] 例如下面的一段内容: 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这一句话比较不好匹配,只能匹配成一个链接标签
lazyboy_wu 2013-06-07
  • 打赏
  • 举报
回复

<?php

$str = "http://dfhakoadghttp://daidgjiadpugidua";

$reg = '#http://(?:\w(?!http://))+(?:\w(?=http://))?#'; // 其中\w的规则可以改成定义的规则

$matches = array();

preg_match_all($reg, $str, $mathces);

print_r($mathces);
床上等您 2013-06-07
  • 打赏
  • 举报
回复
引用 2 楼 shuiguoqu 的回复:
[quote=引用 1 楼 yangball 的回复:]

//只要保证你的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的前面和连接的后面不一定为空[/quote] 举个例子,最好把你的特殊情况都列一下出来。
shuiguoqu 2013-06-07
  • 打赏
  • 举报
回复
引用 1 楼 yangball 的回复:

//只要保证你的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的前面和连接的后面不一定为空
床上等您 2013-06-07
  • 打赏
  • 举报
回复

//只要保证你的url含有http开头的话,可以不使用正则:
$s = 'http://dfhakoadghttp://daidgjiadpugidua';
$a = explode('http://', $s);
foreach($a as $r) {
	if($r) echo "http://$r", '<br/>';
}
//http://dfhakoadg
//http://daidgjiadpugidua
夏之冰雪 2013-06-07
  • 打赏
  • 举报
回复
<?php 
$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

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧