Jump to content

Only allow 1 array value to be used


perky416

Recommended Posts

Hi guys,

 

I originally posted this in the regex section but as another member kindly showed me the problem is not to do with regex.

 

I use the code bellow to validate that a domain name has a valid tld before submitting it to my site, however i have come across a problem, you can add domains such as "test.com.net" and "test.com.mobi.net.org".

 

$tld_list = array('com','net','org','co.uk', etc..........);

$label = '[\\w][\\w\\.\\-]{0,61}[\\w]'; 
$tld = '[\\w]{1,3}'; 

foreach($lines as $line)
{	
if(preg_match( "/^($label)\\.($tld)$/", $line, $match )  && in_array($match[2], $tld_list ))
{ 
}
else
{
	$error[] = $line . " is not a valid domain!<br />";
}
}

 

Would anybody know how i can make it so that only 1 tld from the array is aloud? So if a user tried to add "test.com" or "test.co.uk" it would let them, however if they tried to add "test.com.net" it would throw up the error.

 

Another member kindly gave me the following however i still cant get it to work, i have been on the php manual but cant seem to get my head around the preg_match with the $match value included.

f(preg_match( "/^($label)\\.($tld)$/", $line, $match ) && (!in_array(substr($match[1], -3), $tld_list)) && in_array($match[2], $tld_list ))

 

All help is greatly appreciated.

Many thanks.

Link to comment
https://forums.phpfreaks.com/topic/235616-only-allow-1-array-value-to-be-used/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.