3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = "23456789ABCDEGHJKMNPQRSTUVXYZabcdeghjkmnpqrstuvxyz"; $excludes = ['cp','cb','ck','c6','c9','rn','rm','mm','co','do','cl','db','qp','qb','dp','ww']; $length = 5; $indexedExcludes = array_combine($excludes, $excludes); $dictionaryArray = str_split($str); function fastCaptcha(array $dictionary, array $excludedSyllables, int $length): string { $captcha = ''; $previousCharacter = ''; $generatedLength = 0; while ($generatedLength < $length) { $randomCharacter = $dictionary[array_rand($dictionary)]; if(array_key_exists($previousCharacter . $randomCharacter, $excludedSyllables)) { continue; } $captcha .= $randomCharacter; $previousCharacter = $randomCharacter; $generatedLength++; } return $captcha; } $excludesRegex = '/' . implode('|', $excludes) . '/'; function fuckedCaptcha(string $dictionary, string $excludedSyllables, int $length): string { do { $code = substr(str_shuffle(str_repeat($dictionary, 3)), 0, $length); } while (preg_match($excludedSyllables, $code)); return $code; } $t0 = microtime(true); for ($i = 0; $i < 100000; $i++) fuckedCaptcha($str, $excludesRegex, $length); $t1 = microtime(true); for ($i = 0; $i < 100000; $i++) fastCaptcha($dictionaryArray, $indexedExcludes, $length); $t2 = microtime(true); echo "regexp : " . ($t1 - $t0) . "\n"; echo "array index: " . ($t2 - $t1) . "\n";

preferences:
26.57 ms | 413 KiB | 5 Q