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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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