Reputation: 786
I have an PHP array of strings it looks like this
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.
);
URLs will be all sorts of forms, what I need is an array of those links. Something like this:
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
);
Upvotes: 1
Views: 169
Reputation: 923
The code for you:
$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);
The regular expression was taken from here and corrected a bit.
Upvotes: 2
Reputation: 4364
You should write a proper regular expression to achieve this. Have a look at this
Upvotes: 0