<?php

$str = "23456789ABCDEGHJKMNPQRSTUVXYZabcdeghjkmnpqrstuvxyz";
$excludes = ['cp','cb','ck','c6','c9','rn','rm','mm','co','do','cl','db','qp','qb','dp','ww'];

$indexedExcludes = array_combine($excludes, $excludes);
$dictionaryArray = str_split($str);

function fastCaptcha(array $dictionary, array $excludedSyllables, int $length): string
{
    $captcha = '';
    $generatedLength = 0;

    while ($generatedLength < $length) {
        $randomCharacter = $dictionary[array_rand($dictionary)];
        $captcha .= $randomCharacter;
        $generatedLength++;
    }

    return $captcha;
}

$excludesRegex = '/' . implode('|', $excludes) . '/';

$shortCaptchas  = [];
$mediumCaptchas = [];
$longCaptchas   = [];
for ($i = 0; $i < 100000; $i++)
    $shortCaptchas[]  = fastCaptcha($dictionaryArray, $indexedExcludes, 2);
    $mediumCaptchas[] = fastCaptcha($dictionaryArray, $indexedExcludes, 5);
    $longCaptchas[]   = fastCaptcha($dictionaryArray, $indexedExcludes, 10);


$t0 = microtime(true);

foreach($shortCaptchas as $captcha) {
	preg_match($excludesRegex, $captcha);
}

$t1 = microtime(true);

foreach($mediumCaptchas as $captcha) {
	preg_match($excludesRegex, $captcha);
}

$t2 = microtime(true);

foreach($longCaptchas as $captcha) {
	preg_match($excludesRegex, $captcha);
}

$t3 = microtime(true);

echo "short : " . ($t1 - $t0) . "\n";
echo "medium: " . ($t2 - $t1) . "\n";
echo "long  : " . ($t3 - $t2) . "\n";