Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Created August 31, 2020 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/0d7aded0cbec06ccbb2c7d0be83478de to your computer and use it in GitHub Desktop.
Save lbvf50mobile/0d7aded0cbec06ccbb2c7d0be83478de to your computer and use it in GitHub Desktop.
Just PHP FUN 090.
<?php
# https://www.codewars.com/kata/5893933e1a88084be10001a3 Domain name validator.
function validate($domain) {
echo "$domain\n";
$len = strlen($domain);
if(253 < $len) {
echo "Too long\n";
return false;
}
$x = explode(".",$domain);
if(2 > count($x)) {
echo "Too short\n";
return false;
}
for($i = 0; $i < count($x)-1; $i+=1){
if(63 < strlen($x[$i])){
echo "To long level\n";
return false;
}
if(! (preg_match('/^[a-z0-9][a-z0-9-]*[a-z0-9]$/i',$x[$i]) || preg_match('/^[a-z0-9]$/i',$x[$i]) )) return false;
}
if(! ((preg_match('/^[a-z0-9][a-z0-9-]*[a-z0-9]$/i',$x[$i]) || preg_match('/^[a-z]$/i',$x[$i]) ) && preg_match('/[a-z-]/i',$x[$i]) )) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment