<?php
function check($w)
{
$t = strlen($w);
if($t < 5 || $w[0] == '-' || $w[$t-1] == '-' || $w[strpos($w, "-") + 1] == '-')
return false;
return true;
}
$a = array ("safety", "safet-", "s-a-fe", "-safet", "s7-45", "s--fs");
$t1 = microtime(true);
$c = 0;
for ($i = 0; $i < 100000; $i ++ ){
foreach ($a as $w)
{
if (check($w))
$c ++;
//echo $w, PHP_EOL;
}
}
$t2 = microtime(true);
echo "time without regex: ", $t2 - $t1, " n=" , $c, PHP_EOL;
$t1 = microtime(true);
$c = 0;
for ($i = 0; $i < 100000; $i ++ ){
foreach ($a as $w)
{
if (preg_match('/^(?=.{5,6}$)[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$/', $w))
$c ++;
//echo $w, PHP_EOL;
}
}
$t2 = microtime(true);
echo "time with regex: ", $t2 - $t1, " n=", $c;
preferences:
40.36 ms | 402 KiB | 5 Q