PHP 8.4.24 Released!

Voting

: min(zero, six)?
(Example: nine)

The Note You're Voting On

nino dot skopac at gmail dot com
8 years ago
strspon and preg_match seem to be equally fast for validating numbers:

<?php

$testValInvalid = 'foobar123^^';
$testValValid = '12346';
$allowedChars = '1234567890';

$t1 = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
    assert(strspn($testValInvalid, $allowedChars) != strlen($testValInvalid));
    assert(strspn($testValValid, $allowedChars) == strlen($testValValid));
}
print 'Time taken for strspon: ' . (microtime(true) - $t1);
print PHP_EOL;

$t1 = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
    assert(preg_match('/^[0-9]+$/', $testValInvalid) === 0);
    assert(preg_match('/^[0-9]+$/', $testValValid));
}

print 'Time taken for preg_match: ' . (microtime(true) - $t1);
print PHP_EOL;

/**
nino-mcb:hosp_web ninoskopac$ php test.php
Time taken for strspon: 3.24165391922
Time taken for preg_match: 3.1820080280304
nino-mcb:hosp_web ninoskopac$ php test.php
Time taken for strspon: 3.1806418895721
Time taken for preg_match: 3.2244551181793
 */
?>

<< Back to user notes page

To Top