Fast search
| From: | René Fournier | Date: | Wed, 17 May 2006 16:37:39 +0000 |
| Subject: | Fast search | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-236358@lists.php.net to get a copy of this message | ||
Looking for suggestions on the most compute-efficient way to search variable-length strings (~200 characters) for the occurrence of one of about 100 possible needles. In other words:
$needles = array ( 1 => "Hello Jim" , 2 => "Yellow Banana" , 3 => "Red Car", ... etc .... (100 elements)
$haystack = "Once upon a time there was a programming language that everyone loved. Its name was PHP, and the people that worked with it also liked Yellow Bananas. One day...";
Now perhaps I'm blind, but I can't see an obvious string or array function that simply allows you to use an array as a needle while searching a string haystack. What I'm really looking for is something like the reverse of in_array.
The obvious thing would be to loop through the needles and run substr_count on the haystack... But is there a faster way?
...Rene