PHP:正则表达式从字符串获取URL [英] PHP: Regular Expression to get a URL from a string

查看:379
本文介绍了PHP:正则表达式从字符串获取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项:
识别字符串中是否存在URL
Php解析链接/电子邮件

Possible Duplicates:
Identifying if a URL is present in a string
Php parse links/emails

我正在研究一些PHP代码,这些代码需要来自各种来源的输入,并且需要查找URL并将其保存在某处.需要处理的输入种类如下:

I'm working on some PHP code which takes input from various sources and needs to find the URLs and save them somewhere. The kind of input that needs to be handled is as follows:

http://www.youtube.com/watch?v=IY2j_GPIqRA
Try google: http://google.com! (note exclamation mark is not part of the URL)
Is http://somesite.com/ down for anyone else?

输出:

http://www.youtube.com/watch?v=IY2j_GPIqRA
http://google.com
http://somesite.com/

我已经从互联网上借用了一个可运行的正则表达式,但不幸的是,它删除了查询字符串-不好!

I've already borrowed one regular expression from the internet which works, but unfortunately wipes the query string out - not good!

任何帮助将正则表达式组合在一起的方法,或者可能是该问题的另一种解决方案,将不胜感激.

Any help putting together a regular expression, or perhaps another solution to this problem, would be appreciated.

推荐答案

Jan Goyvaerts,Regex Guru,

Jan Goyvaerts, Regex Guru, has addressed this issue in his blog. There are quite a few caveats, for example extracting URLs inside parentheses correctly. What you need exactly depends on the "quality" of your input data.

对于您提供的示例,\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]在不区分大小写的模式下使用时有效.

For the examples you provided, \b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$] works when used in case-insensitive mode.

要在多行字符串中查找所有匹配项,请使用

So to find all matches in a multiline string, use

preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

这篇关于PHP:正则表达式从字符串获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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