开发者

Extract http://www.website.com from http://www.website.com/08/2010/super-cool-article

I suck at regex, I only manage开发者_运维知识库d to get so far preg_match("/http:\/\//", $url).

I need this for a php script


$parts = parse_url('hotpotatoes://asd.com');
return $parts['scheme'].'://'.$parts['host'];


Or by using regex:

<?php
$blah="http://www.website.com/08/2010/super-cool-article";
preg_match('/^http:\/\/(\w|\.)*/i',$blah,$matches);
$result=$matches[0];
echo $result;
?>

or by an explosion:

<?php
$blah="http://www.website.com/08/2010/super-cool-article";
$blah=explode("/",$blah);
$result=$blah[0]."//".$blah[2];
echo $result;
?>


An alternative expression would be /^http:\/\/[^\/]++/. ++ is used because a possessive quantifier is more efficient.

preg_match("/^http:\/\/[^\/]++/", 
           "http://www.website.com/08/2010/super-cool-article", 
           $matches);
echo($matches[0]); // "http://www.website.com"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜