How do I convert random domain names to consistent lowercase URLs?
I have this function in a class:
protected $supportedWebsitesUrls = ['www.youtube.com', 'www.vimeo.com', 'www.dailymotion.com'];
protected function isValid($videoUrl)
{
$urlDetails = parse_url($videoUrl);
if (in_array($urlDetails['host'], $this->supportedWebsitesUrls))
{
return true;
} else {
throw new \Exception('This website is not supported yet!');
return false;
}
}
It basically extracts the hostname from any random url and then checks if it is in the array $supportedWebsitesUrls
to make sure it is on a supported website. But if I add, say: dailymotion.com
instead www.dailymotion.com
, it won't detect this url. Also if I try to do WWW.DAILYMOTION.COM it still doesn't work. What can be done? Please help me.
source to share
You can use the preg_grep function for this. preg_grep
supports regular expressions for a given array.
Using example:
$supportedWebsitesUrls = array('www.dailymotion.com', 'www.youtube.com', 'www.vimeo.com');
$s = 'DAILYMOTION.COM';
if ( empty(preg_grep('/' . preg_quote($s, '/') . '/i', $supportedWebsitesUrls)) )
echo 'This website is not supported yet!\n';
else
echo "found a match\n";
Output:
found a match
source to share
You can perform multiple checks on it:
For lower case to upper case, the php function strtolower()
will sort you.
how to check with www. at the beginning and without it, you can add an extra check to your if clause;
if (in_array($urlDetails['host'], $this->supportedWebsitesUrls) || in_array('www.'.$urlDetails['host'], $this->supportedWebsitesUrls))
source to share