Question: I want to extract the website name, from a link, so I write the following function:

```
protected function getWebsiteName()
{
    $prefixs = ['https://', 'http://', 'www.'];

    foreach($prefixs as $prefix)
    {
        if(strpos($this->website_link, $prefix) !== false)
        {
            $len = strlen($prefix);
            $this->website_name = substr($this->website_link, $len);
            $this->website_name = substr($this->website_name, 0, strpos($this->website_name, '.'));
        }
    }
}

```

The problem is that when I use I website link that look like **<https://www.github.com>**, the result is: **s://www**, and the function only works when I remove that 'www.' from the array list.

Any ideas why this is happening, or how I can improve this function?

Answer: 
You could use `parse_url();`, Try:

```
print_r(parse_url('https//www.name/'));

```
Yes, use parse\_url along with preg\_match should do the job

```
function getWebsiteName($url)
{
  $pieces = parse_url($url);
  $domain = isset($pieces['host']) ? $pieces['host'] : '';
  if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
    return $regs['domain'];
  }
  return false;
}

```

This is fixing your code.

```
function getWebsiteName()
{
    $this->website_name = $this->website_link;
    $prefixs = array('https://', 'http://', 'www.');

    foreach($prefixs as $prefix)
    {
      if (substr($this->website_name, 0, strlen($prefix)) == $prefix) {
        $this->website_name = substr($this->website_name, strlen($prefix));
      }              
    }
}

```