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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.