怎麼把字串中所有的網址都抓出來
function getUrlsFromString($string){
$result = preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $matches);
if( empty($result) ){ // 沒抓到就回傳 0
return 0;
}else{
return $matches[0]; // 抓到回傳 url array
}
return $matches;
}
$data = file_get_contents('http://facebook.com');
print_r(getUrlsFromString($data)) ;
結果會類似這樣
Array
(
[0] => https://www.facebook.com/unsupportedbrowser
[1] => https://www.facebook.com/unsupportedbrowser
[2] => https://static.xx.fbcdn.net/rsrc.php/yz/r/KFyVIAWzntM.ico
[3] => https://static.xx.fbcdn.net/rsrc.php/v3/yB/l/0
[4] => https://static.xx.fbcdn.net/rsrc.php/v3/yF/l/0
[5] => https://static.xx.fbcdn.net/rsrc.php/v3/yQ/l/0
[6] => https://static.xx.fbcdn.net/rsrc.php/v3/y-/l/0
)