2013-03-21 76 views
0

如果有人知道什麼是最好的方法是提取從另一個環節一個環節我想知道,這裏有一個例子:我如何從較長的鏈接使用PHP提取鏈接?

如果我有以下格式鏈接:

http://www.youtube.com/watch?v=35HBFeB4jYg OR 
http://it.answers.yahoo.com/question/index?qid=20080520042405AApM2Rv OR 
https://www.google.it/search?q=rap+tedesco&aq=f&oq=rap+tedesco&aqs=chrome.0.57j62l2.2287&sourceid=chrome&ie=UTF-8#hl=en&sclient=psy-ab&q=migliori+programatori&oq=migliori+programatori&gs_l=serp.3..0i19j0i13i30i19l3.9986.13880.0.14127.14.10.0.4.4.0.165.931.6j4.10.0...0.0...1c.1.7.psy-ab.tPmiWRyUVXA&pbx=1&bav=on.2,or.r_cp.r_qf.&fp=ffc0e9337f73a744&biw=1280&bih=699 

我怎麼會去大約只提取網頁像這樣:

http://www.youtube.com 
http://it.answers.yahoo.com 
https://www.google.it 

我在想,如果什麼的正則表達式我可以使用PHP來實現這一點,也正則表達式的路要走?

回答

1

使用功能parse_url

$link = "https://www.google.it/search?q=rap+tedesco"; 
$parseUrl = parse_url($link); 
$siteName = $parseUrl['scheme']."://". $parseUrl['host']; 

使用正則表達式

preg_match('@http(s?)://([\w]+\.){1}([\w]+\.?)[email protected]',$link,$matches); 
echo $matches[0]; 

Codeviper Demo.

2

有解析URL的PHP​​函數:parse_url

$url = 'http://it.answers.yahoo.com/question/index?qid=20080520042405AApM2Rv'; 
$p = parse_url($url); 
echo $p["scheme"] . "// . "$p["host"]; 
0

你只是想在頁面的域名,在PHP中存在一個名爲parse_url功能,可以幫助