解析字符串以获取URL数组

时间:2010-07-13 21:25:30

标签: regex string url arrays parsing

想象一下,我有一个字符串 - 像这样:

This is some really cool text about http://google.com/ and it also contains some urls like http://apple.com/ and its very nice! This is too long and I need to do some magic stuff to fix this very big problem. Oh no.

正如你所看到的,字符串中有两个URL,不知何故,假设我需要某种REGEX,我需要获取这些URL的数组,以便我可以操作它们。像这样......

Array()

- [0] = 'http://google.com/'
- [1] = 'http://apple.com/'

所有帮助赞赏:) 谢谢!

3 个答案:

答案 0 :(得分:2)

https?:\/\/[^\s]+

找到以http://或https://开头的内容,然后拉出字符直到找到空格。

答案 1 :(得分:2)

我认为这也应该有用。

$string = 'ajskldfj http://google.ca jslfkjals s http://www.apple.com jalksf';
$pattern = '/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/';
preg_match_all($pattern, $string, $matches);

答案 2 :(得分:0)

您需要一个如下所示的正则表达式模式:

'https?:\/\/(\w*?.)?(?P<domain>\w+).(\w+)/'