note 46236 deleted from function.substr-count by nlopess

From: Date: Thu, 23 Jun 2005 19:59:42 +0000
Subject: note 46236 deleted from function.substr-count by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-90856@lists.php.net to get a copy of this message
Note Submitter: jsb17 at cornell dot edu ---- In response to Jesse at bend dot com's comment regarding case insensitivity, if your substr_count() function is in a loop, never implement this: <?php // for each needle for($i=0; $<$n_needles; $i++){ // duplicating strtolower() process time on each iteration! OUCH! substr_count(strtolower($haystack), strtolower($needle[$i])); } // for each needle ?> I recommend implementing this instead: <?php // initiate lowercase instances of search text and needles $haystack_lower = strtolower($haystack); for ($i=0; $<$n_needles; $i++){$needle[$i] = strtolower($needle[$i]);} // after exploding // for each needle for($i=0; $i<$n_needles; $i++){ substr_count($haystack_lower, $needle[$i]); // assign this to whatever... } // for each needle ?> An example of the aforementioned arises if you are implementing, for instance, a search function in a help manual, when you check each user-entered search word against the manual text (like at the top of this page), and count the number of occurrences of each search word to weigh your results so you can display the best results first. If the above code is within ANOTHER loop traversing each of many manual pages, then you'll definitely get hammered on the iterative strtolower() process time. Always try to keep strtolower() outside the outermost loop, and query using use "SELECT LOWER(manual_text) FROM..." instead. Maybe this comment belongs in the strtolower() documentation?

« previous php.notes (#90856) next »