Difference between revisions of "User:SignpostMarv Martin/is uuid comparison"
(→no regex: correcting logic error) |
|||
Line 20: | Line 20: | ||
} | } | ||
else if(strpos($string,'-') !== 8) | else if(strpos($string,'-') !== 8) | ||
{ | |||
$match = false; | |||
} | |||
else if(strpos($string,'-',24) !== false) | |||
{ | |||
$match = false; | |||
} | |||
else if(strpos($string,'-',9) !== 13) | |||
{ | |||
$match = false; | |||
} | |||
else if(strpos($string,'-',13) !== 13) | |||
{ | |||
$match = false; | |||
} | |||
else if(strpos($string,'-',14) !== 18) | |||
{ | |||
$match = false; | |||
} | |||
else if(strpos($string,'-',18) !== 18) | |||
{ | |||
$match = false; | |||
} | |||
else if(strpos($string,'-',19) !== 23) | |||
{ | |||
$match = false; | |||
} | |||
else if(strpos($string,'-',23) !== 23) | |||
{ | { | ||
$match = false; | $match = false; | ||
} | } | ||
else if(ctype_alnum(str_replace('-','',$string)) === false) | else if(ctype_alnum(str_replace('-','',$string)) === false) | ||
{ | |||
$match = false; | |||
} | |||
else if(($translated = strtr($string,array( | |||
'-'=>'', | |||
'0'=>'', | |||
'1'=>'', | |||
'2'=>'', | |||
'3'=>'', | |||
'4'=>'', | |||
'5'=>'', | |||
'6'=>'', | |||
'7'=>'', | |||
'8'=>'', | |||
'9'=>'', | |||
'a'=>'', | |||
'b'=>'', | |||
'c'=>'', | |||
'd'=>'', | |||
'e'=>'', | |||
'f'=>'', | |||
))) !== false && strlen($translated) !== 0) | |||
{ | |||
$match = false; | |||
} | |||
return $match; | |||
} | |||
</php> | |||
== no regex or ctype == | |||
<php> | |||
function is_uuid_no_regex_ctype($string) | |||
{ | |||
$match = true; | |||
if(strlen($string) !== 36) | |||
{ | |||
$match = false; | |||
} | |||
else if(strpos($string,'-') !== 8) | |||
{ | { | ||
$match = false; | $match = false; | ||
Line 52: | Line 119: | ||
} | } | ||
else if(strpos($string,'-',23) !== 23) | else if(strpos($string,'-',23) !== 23) | ||
{ | |||
$match = false; | |||
} | |||
else if(($translated = strtr($string,array( | |||
'-'=>'', | |||
'0'=>'', | |||
'1'=>'', | |||
'2'=>'', | |||
'3'=>'', | |||
'4'=>'', | |||
'5'=>'', | |||
'6'=>'', | |||
'7'=>'', | |||
'8'=>'', | |||
'9'=>'', | |||
'a'=>'', | |||
'b'=>'', | |||
'c'=>'', | |||
'd'=>'', | |||
'e'=>'', | |||
'f'=>'', | |||
))) !== false && strlen($translated) !== 0) | |||
{ | { | ||
$match = false; | $match = false; |
Revision as of 12:12, 29 July 2008
regex
<php> function is_uuid_regex($string) { return (bool)preg_match( '/^([0-9a-f]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12})$/S' ,$string); } </php>
no regex
<php> function is_uuid_no_regex($string) { $match = true; if(strlen($string) !== 36) { $match = false; } else if(strpos($string,'-') !== 8) { $match = false; } else if(strpos($string,'-',24) !== false) { $match = false; } else if(strpos($string,'-',9) !== 13) { $match = false; } else if(strpos($string,'-',13) !== 13) { $match = false; } else if(strpos($string,'-',14) !== 18) { $match = false; } else if(strpos($string,'-',18) !== 18) { $match = false; } else if(strpos($string,'-',19) !== 23) { $match = false; } else if(strpos($string,'-',23) !== 23) { $match = false; } else if(ctype_alnum(str_replace('-',,$string)) === false) { $match = false; } else if(($translated = strtr($string,array( '-'=>, '0'=>, '1'=>, '2'=>, '3'=>, '4'=>, '5'=>, '6'=>, '7'=>, '8'=>, '9'=>, 'a'=>, 'b'=>, 'c'=>, 'd'=>, 'e'=>, 'f'=>, ))) !== false && strlen($translated) !== 0) { $match = false; } return $match; } </php>
no regex or ctype
<php> function is_uuid_no_regex_ctype($string) { $match = true; if(strlen($string) !== 36) { $match = false; } else if(strpos($string,'-') !== 8) { $match = false; } else if(strpos($string,'-',24) !== false) { $match = false; } else if(strpos($string,'-',9) !== 13) { $match = false; } else if(strpos($string,'-',13) !== 13) { $match = false; } else if(strpos($string,'-',14) !== 18) { $match = false; } else if(strpos($string,'-',18) !== 18) { $match = false; } else if(strpos($string,'-',19) !== 23) { $match = false; } else if(strpos($string,'-',23) !== 23) { $match = false; } else if(($translated = strtr($string,array( '-'=>, '0'=>, '1'=>, '2'=>, '3'=>, '4'=>, '5'=>, '6'=>, '7'=>, '8'=>, '9'=>, 'a'=>, 'b'=>, 'c'=>, 'd'=>, 'e'=>, 'f'=>, ))) !== false && strlen($translated) !== 0) { $match = false; } return $match; } </php>