从php字符串返回URLS

时间:2011-07-21 09:19:48

标签: php string url

我有一个PHP字符串数组,看起来像这样

Array
(
    [1] => Lorem ipsum dolor sit amet http://www.google.com/search?q=stackoverflow consectetur adipiscing elit.
    [2] => Phasellus tempor vehicula fringilla. www.google.com/search?q=stackoverflow&ie=utf-8
    [3] => google.com/search?q=stackoverflow&ie=utf-8 Aenean in cursus libero.
);

网址将是各种形式,我需要的是这些链接的数组。像这样:

Array
(
    [1] => http://www.google.com/search?q=stackoverflow
    [2] => http://www.google.com/search?q=stackoverflow&ie=utf-8
    [3] => http://www.google.com/search?q=stackoverflow&ie=utf-8
);

2 个答案:

答案 0 :(得分:2)

代码:

$pattern = '/((https?|ftp)\:(\/\/)|(file\:\/{2,3}))?(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(((([a-zA-Z0-9]+)(\.)?)+)(\.)(com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|[a-z]{2}))([\/][\/a-zA-Z0-9\.]*)*([\/]?(([\?][a-zA-Z0-9]+[\=][a-zA-Z0-9\%\(\)]*)([\&][a-zA-Z0-9]+[\=][a-zA-Z0-9\%\(\)]*)*))?/';

$a = array(
    'Lorem ipsum dolor sit amet http://www.google.com/search?q=stackoverflow consectetur adipiscing elit.',
    'Phasellus tempor vehicula fringilla. www.google.com/search?q=stackoverflow&ie=utf-8',
    'google.com/search?q=stackoverflow&ie=utf-8 Aenean in cursus libero.',
);

$urls = array();

foreach($a as $line)
{
    if(!preg_match($pattern, $line, $match))
        continue;

    $urls[] = $match[0];
}

var_dump($urls);

正则表达式取自here并稍作修正。

答案 1 :(得分:0)

你应该写一个合适的正则表达式来实现这一点。看看this