I am looking for a function like strpos() with two significant differences:
- To be able to accept multiple needles. I mean thousands of needles at ones.
- To search for all occurrences of the needles in the haystack and to return an array of starting positions.
Of course it has to be an efficient solution not just a loop through every needle. I have searched through this forum and there were similar questions to this one, like:
- Using an array as needles in strpos
- Define multiple needles using stripos
- Can't search an array in PHP in_array for the presence of multiple needles
but nether of them was what I am looking for. I am using strpos just to illustrate my question better, probably something entirely different has to be used for this purpose.
I am aware of Zend_Search_Lucene and I am interested if it can be used to achieve this and how (just the general idea)?
Thanks a lot for Your help and time!
try preg match for multiple
Checking for multiple strpos values
It seems you are searching for whole words. In this case, something like this might help. As it uses built-in functions, it should be faster than custom code, but you have to profile it:
Storing the information (like the needles) in the right format will improve the runtime ( e.g. as you don't have to call
array_flip
).Note from the
str_word_count
documentation:So make sure you set the locale right.
Here's some sample code for my strategy:
I've implemented a simple brute force method above that will work with any combination of needles and haystacks (not just words). For possibly faster algorithms check out:
Other Solution
You could use a regular expression, they support OR operations. This would however make it fairly slow, compared to strpos.
I know this doesn't answer the OP's question but wanted to comment since this page is at the top of Google for strpos with multiple needles. Here's a simple solution to do so (again, this isn't specific to the OP's question - sorry):
If 2 items are added to the $missing array that means that the input doesn't satisfy any of the image formats in the $img_formats array. At that point you know that you can return an error, etc. This could easily be turned into a little function:
Back to our first example using then the function instead:
Of course, what you do after the function returns true or false is up to you.