在Array上运行查询

I want to run a query over an Array or string with PHP

Let say I got a string:

$Iamastring = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc velit est, porta sed feugiat vitae, sodales et nisl. Suspendisse ut."

And I want to run a query over that string to find it:

"Lorem ipsum" OR ("dolor" AND "sodales")

In this case it would be true :). How is the best way to do this?

regexp :) preg_match('\(Lorem ipsum)|(dolor.+sodales)\', $matches). Or variations with different regexp's

You would do this with a regular expression. Check out something like preg_match()

The following should return true or false if the pattern matches:

preg_match ('/(Lorem ipsum)|(dolor+sodales)/' $Iamastring);

RegExp is one way, though if you want to use inbuilt string functions:

$haystack="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc velit est, porta sed feugiat vitae, sodales et nisl. Suspendisse ut.";

if((strrpos($haystack, "Lorem Ipsum")!==false)||((strrpos($haystack, "dolor")!==false)&&(strrpos($haystack, "sales")!==false))){

// do this if true

}

Note that PHPs inbuilt string functions are faster than RegExps, but each approach varies in appropriateness depending on context.