User:SignpostMarv Martin/is uuid comparison

From Second Life Wiki
< User:SignpostMarv Martin
Revision as of 13:28, 29 July 2008 by SignpostMarv Martin (talk | contribs) (noticed ctype_xdigit was for hexadecimal-fu)
Jump to navigation Jump to search

regex

<php> function is_uuid_regex($string) { return (bool)preg_match( '/^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{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_xdigit(str_replace('-',,$string)) === false) { $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>