tags:

views:

68

answers:

3

I suck at regex, I only managed to get so far preg_match("/http:\/\//", $url).

I need this for a php script

+6  A: 
$parts = parse_url('hotpotatoes://asd.com');
return $parts['scheme'].'://'.$parts['host'];
Coronatus
parse_url ftw. But you shouldn't have mentioned hot potatoes. That's quite harsh of you. No i have to stop work and go get some. If I lose my job for this, I'm gonna find you!
gAMBOOKa
+1  A: 

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;
?>
Prab
A: 

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"
sirhc