Jump to content

[SOLVED] Stripping a Domain


.Darkman

Recommended Posts

Hi,

 

I need a small help.

How to strip a domain name alone from an URL.

 

For eg, User enters http://www.phpfreaks.com/forums/index.php?action=post;board=1.0 in a field.

I need that to be converted into http://www.phpfreaks.com/

 

How do i do it ?

 

Also how do i make so that http://phpfreaks.com/ is converted into http://www.phpfreaks.com/

 

 

Thanks,

 

Link to comment
https://forums.phpfreaks.com/topic/43438-solved-stripping-a-domain/
Share on other sites

$str = 'phpfreaks.com';
if (strpos($str, 'www') !== 0) {
  # !== 0 fails only if 'www' is found at the start of $str
  $str = "www.$str";
}

 

I deliberately searched for 'www' instead of 'www.', to allow for cases like www1, www2.  I often use a regex like this instead

 

if (!preg_match('|www[0-9]*\.|', $str))

 

That one matches

 

www.

www1.

www50.

 

And all other numbers following www

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.