note 57114 added to function.readdir

From: Date: Sat, 24 Sep 2005 09:31:28 +0000
Subject: note 57114 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-95697@lists.php.net to get a copy of this message
To beanz at gangstersonline dot co dot uk, just a little optimization tip. You have make a lot of eregi calls in a sequence of elseif statements. That means that for a worst case, you'll be making 8 eregi calls per iteration. Instead of using a bunch of conditional eregi() calls (which is much slower compared to preg_match or strpos), you could do the following: <?php if ($i) //create a mapping array $ext_map = array('exe' => 'exe', 'bmp' => 'image1', etc....) foreach($tab as $rep) { //use one preg_match to grab the extention if (preg_match('/\.([a-z]+)$/', $rep, $matches) { $ext = $ext_map[$matches[1]]; } else { $ext = 'text'; } //rest of code... } ?> So instead of a worst case of n eregi calls per iteration, you're guaranteed only one preg_match per call. Or possibly even faster than preg_match: $ext = $ext_map[substr($rep, strrpos($rep, '.'))]; ---- Manual Page -- http://www.php.net/manual/en/function.readdir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57114 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57114&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57114&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57114&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57114&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57114&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57114&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57114&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57114&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95697) next »