开发者

PHP: Regular Expression to get a URL from a string [duplicate]

This question already has answers here: 开发者_JS百科 Closed 12 years ago.

Possible Duplicates:

Identifying if a URL is present in a string

Php parse links/emails

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?

Output:

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, 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.

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];


Why not try this one. It is the first result of Googling "URL regular expression".

((https?|ftp|gopher|telnet|file|notes|ms-help):((\/\/)|(\\\\))+[\w\d:#@%\/;$()~_?\+-=\\\.&]*)

Not PHP, but it should work, I just slightly modified it by escaping forward slashes.

source

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜