Return URLS from php string -
i have php array of strings looks 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 sorts of forms, need array of links. 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 );
the code 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 $line) { if(!preg_match($pattern, $line, $match)) continue; $urls[] = $match[0]; } var_dump($urls);
the regular expression taken here , corrected bit.
Comments
Post a Comment