perky416 Posted May 5, 2011 Share Posted May 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235616-only-allow-1-array-value-to-be-used/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.