Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strpos() with multiple needles?

Tags:

I am looking for a function like strpos() with two significant differences:

  1. To be able to accept multiple needles. I mean thousands of needles at ones.
  2. 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!

like image 770
Nikola Obreshkov Avatar asked Aug 01 '11 09:08

Nikola Obreshkov


People also ask

What is the strpos () function used for?

strpos in PHP is a built-in function. Its use is to find the first occurrence of a substring in a string or a string inside another string. The function returns an integer value which is the index of the first occurrence of the string.

What is strpos ()? Give example?

The strpos() function finds the position of the first occurrence of a string inside another string. The stripos() function finds the position of the first occurrence of a string inside another string. 2. It is case-sensitive function.

What does Strpos return if not found?

Return Value: Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found.

How do you check if a string contains a character PHP?

You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .


1 Answers

try preg match for multiple

if (preg_match('/word|word2/i', $str)) 

Checking for multiple strpos values

like image 85
user3270784 Avatar answered Sep 30 '22 19:09

user3270784