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?