Difference between revisions of "User:Niaht Nakamichi/PHP Code"
m (→is_uuid) |
m (→is_uuid) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
function is_uuid($uuid) { | function is_uuid($uuid) { | ||
if (strlen($uuid) !== 36) return false; | if (strlen($uuid) !== 36) return false; | ||
if (strpos($uuid, '-') !== 8) return false; | if (strpos($uuid, '-') !== 8) return false; | ||
if (strpos($uuid, '-', 24) !== false) return false; | |||
if (strpos($uuid, '-', 9) !== 13) return false; | if (strpos($uuid, '-', 9) !== 13) return false; | ||
if (strpos($uuid, '-', 13) !== 13) return false; | |||
if (strpos($uuid, '-', 14) !== 18) return false; | if (strpos($uuid, '-', 14) !== 18) return false; | ||
if (strpos($uuid, '-', 18) !== 18) return false; | |||
if (strpos($uuid, '-', 19) !== 23) return false; | if (strpos($uuid, '-', 19) !== 23) return false; | ||
if (strpos($uuid, '-', 23) !== 23) return false; | |||
if (ctype_xdigit(str_replace('-', '', $uuid)) == false) return false; | if (ctype_xdigit(str_replace('-', '', $uuid)) == false) return false; | ||
return true; | return true; | ||
} | } | ||
</php> | </php> | ||
A somewhat slower version using substr_count: | |||
<php> | |||
function is_uuid2($uuid) { | |||
if (strlen($uuid) !== 36) return false; | |||
if (substr_count($uuid, '-') !== 4) return false; | |||
if (strpos($uuid, '-') !== 8) return false; | |||
if (strpos($uuid, '-', 9) !== 13) return false; | |||
if (strpos($uuid, '-', 14) !== 18) return false; | |||
if (strpos($uuid, '-', 19) !== 23) return false; | |||
if (ctype_xdigit(str_replace('-', '', $uuid)) == false) return false; | |||
return true; | |||
} | |||
</php> | |||
== Results == | |||
<pre> | |||
time index ex time % | |||
Start 1217369864.26464800 - 0.00% | |||
double strpos checks 1217369864.34994000 0.085292 50.32% | |||
substr_count 1217369864.43412200 0.084182 49.67% | |||
Stop 1217369864.43413600 0.000014 0.01% | |||
total - 0.169488 100.00% | |||
</pre> |
Latest revision as of 14:17, 29 July 2008
is_uuid
<php> function is_uuid($uuid) { if (strlen($uuid) !== 36) return false; if (strpos($uuid, '-') !== 8) return false; if (strpos($uuid, '-', 24) !== false) return false; if (strpos($uuid, '-', 9) !== 13) return false; if (strpos($uuid, '-', 13) !== 13) return false; if (strpos($uuid, '-', 14) !== 18) return false; if (strpos($uuid, '-', 18) !== 18) return false; if (strpos($uuid, '-', 19) !== 23) return false; if (strpos($uuid, '-', 23) !== 23) return false; if (ctype_xdigit(str_replace('-', , $uuid)) == false) return false; return true; } </php>
A somewhat slower version using substr_count:
<php> function is_uuid2($uuid) { if (strlen($uuid) !== 36) return false; if (substr_count($uuid, '-') !== 4) return false; if (strpos($uuid, '-') !== 8) return false; if (strpos($uuid, '-', 9) !== 13) return false; if (strpos($uuid, '-', 14) !== 18) return false; if (strpos($uuid, '-', 19) !== 23) return false; if (ctype_xdigit(str_replace('-', , $uuid)) == false) return false; return true; } </php>
Results
time index ex time % Start 1217369864.26464800 - 0.00% double strpos checks 1217369864.34994000 0.085292 50.32% substr_count 1217369864.43412200 0.084182 49.67% Stop 1217369864.43413600 0.000014 0.01% total - 0.169488 100.00%