2013-03-21 77 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功能,可以帮助