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

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

问题描述

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

我正在编写一些 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, 在他的博客中解决了这个问题.有很多注意事项,例如正确提取括号内的 URL.您需要什么完全取决于输入数据的质量".

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.

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

For the examples you provided, (?:(?: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('/(?:(?: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天全站免登陆