You need to parse the domain from the URL. See this answer for an explanation - Parsing Domain From URL In PHP
<?php
$url = 'http://example.com/index.html';
$parse = parse_url($url);
$domain = $parse['host'];
$domains = array('example.com','example2.com','example3.com');
if (in_array($domain, $domains)) {
...
}
EDIT - Not 100% certain on the requirements here but sounds like you want a function that returns a boolean.
Need only to check if url is with domain from list or not. If yes
write in db if not return error
/**
* Returns a boolean indicating if the given URL is in a list of valid domains.
* @param string $url
* @return bool
*/
function validDomain($url)
{
$parse = parse_url($url);
$domain = $parse['host'];
$validDomains = array('example.com', 'example2.com', 'example3.com');
return in_array($domain, $validDomains);
}
if (validDomain('http://example.com/index.html')) {
// Insert into DB.
} else {
// Handle your error.
}