Re: If the first four characters are "0000", then do {}

From: Date: Tue, 26 Jan 2010 16:21:23 +0000
Subject: Re: If the first four characters are "0000", then do {}
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-301480@lists.php.net to get a copy of this message
On Mon, Jan 25, 2010 at 22:51, Daevid Vincent <daevid@daevid.com> wrote: >> -----Original Message----- >> From: parasane@gmail.com [mailto:parasane@gmail.com] On >> Behalf Of Daniel Brown >> Sent: Monday, January 25, 2010 6:43 PM >> To: John Taylor-Johnston >> Cc: PHP-General >> Subject: Re: [PHP] If the first four characters are "0000", then do {} >> >> On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston >> <John.Taylor-Johnston@cegepsherbrooke.qc.ca> wrote: >> > I am reading the manual: >> > http://ca.php.net/manual/en/ref.strings.php >> > >> > $mydata->restored = "0000-00-00"; >> >> <?php >> >> $o[] = '0942-23-23'; >> $o[] = '0000-00-00'; >> $o[] = '1238-00-00'; >> $o[] = '0001-23-45'; >> $o[] = '0000-11-22'; >> >> for($i=0;$i<count($o);$i++) { >>         if(preg_match('/^[0]{4,}\-/U',$o[$i])) { >>                 echo "Offset #".$i." matches: ".$o[$i].PHP_EOL; >>         } >> } >> ?> > > Holy macaroni. Talk about overkill! > > if (substr($mydata->restored,0,4) == "0000") { } Overkill? <?php $o[] = '0942-23-23'; $o[] = '0000-00-00'; $o[] = '1238-00-00'; $o[] = '0001-23-45'; $o[] = '0000-11-22'; $now = microtime(); for($i=0;$i<count($o);$i++) { if(preg_match('/^[0]{4,}\-/U',$o[$i])) { //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL; } } echo (microtime(1) - $now)."\n"; $later = microtime(); for($i=0;$i<count($o);$i++) { if(substr($o[$i],0,4) == "0000") { //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL; } } echo (microtime(1) - $later)."\n"; ?> Sample Output: 1264522257.0001 1264522257 The preg_match() method, which is more expandable and adaptable than relying on static position, took less than one-ten-thousandths of a second longer to calculate than substr(). Just an FYI before you start worshipping pasta, Mr. Vincent. ;-P -- </Daniel P. Brown> daniel.brown@parasane.net || danbrown@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Looking for hosting or dedicated servers? Ask me how we can fit your budget!

« previous php.general (#301480) next »