Jump to content

getting the domain of a url by parse_url


etrader

Recommended Posts

I have two questions:

 

1. Is it the best way to get the domain of a url by parse_url? or preg_match or str_replace can be better alternatives?

 

2. What is the best way to remove www from the domains? Note that the domain name itself may contain www too (like www.1www.com)

 

Thank for your attention  :shy:

Link to comment
Share on other sites

1. parse_url is by far the best solution. Avoid preg_match whenever you can.

2.

$domain = ltrim(parse_url($url, PHP_URL_HOST), 'w.'); /*magic*/

 

This can become an issue if your domain is http://www.com (valid host name) but you can solve that like this:

 

$domain = 'www.' . ltrim(parse_url($url, PHP_URL_HOST), 'w.'); /*magic*/

 

This will do the following:

 

www.com =ltrim=> com => www.com
domain.top =ltrim=> domain.top => www.domain.top
www.domain.top =ltrim=> domain.top => www.domain.top

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.