Re: Fast search

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, May 17, 2006 11:37 am, René Fournier wrote:
> 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?

//Kind of a hack, I guess, but might be decently fast:
$pattern = implode('|', $needles);
if (preg_match("/($pattern)/", $haystack, $matches)){
  var_dump($matches);
}

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux