用PHP查找文本中的所有URL(链接) [英] find all urls (links) in text with php

查看:344
本文介绍了用PHP查找文本中的所有URL(链接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码正则表达式,可以将所有不同的url转换为某些文本中的链接.

I have this code regex, which should transform all kind of diffrent urls into links in some text.

preg_replace代码为:

The preg_replace code is:

$regex = '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@';
$text = preg_replace($regex, '<a href="$1">$1</a>', $item);

现在它适用于您可以想象的几乎所有URL,但是我遇到的问题是逗号和URL中的特殊字符...

now it works for almost all URLs you can imagine, but the problems i have are commas, and special characters in URLs...

问题在于我:

http://www.sdfsdfsdf.sd/si/391 ,1000,1/more.html

http://sdfsddsdf-sdfsdfds.sr/组件/选项,com_contact/Itemid,3/lang,si/

在stackoverflow上,这两个都很好,很有趣:)

Funny here at stackoverflow those two are OK :)

致以最诚挚的问候,

推荐答案

您必须稍微修改一下正则表达式.这样就可以了:

You have to edit your regex a bit. This will do the job:

$regex = '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.\,]*(\?\S+)?)?)*)@';

如您所见,在这里[-\w/_\.\,]添加了一个逗号,仅此而已.

As you can see, there's a comma added here [-\w/_\.\,] and nothing more.

享受!

这篇关于用PHP查找文本中的所有URL(链接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆