|
Posted by Mike P2 on 05/11/07 21:34
I don't understand what's wrong with:
if( trim( $variable ) == '' )
I just benchmarked it, it's faster than the regex methods.
By the way, I think the best regex to search with would be '#\S#',
because that would indicate that the string contains at least one non-
whitespace character anywhere in the string. That is the pattern I
used in the benchmarking.
<?php //quick benchmark test
$t1 = 'asdfasdf ';
$t2 = ' ';
$i = 0;
$tTrim = microtime( true );
for( ; $i < 10000; ++$i )
{
trim( $t1 ) == '';
trim( $t2 ) == '';
}
$tTrim = microtime( true ) - $tTrim;
$i = 0;
$tRegEx = microtime( true );
for( ; $i < 10000; ++$i )
{
preg_match( '#\S#', $t1 );
preg_match( '#\S#', $t2 );
}
$tRegEx = microtime( true ) - $tRegEx;
printf( "trim: %f<br />regex: %f", $tTrim, $tRegEx );
?>
Besides, although I usually place little value in this, I think it's
easier to read ( trim( $var ) == '' ) than to interpret a regex
pattern.
-Mike PII
[Back to original message]
|