PHP and regular expressions: is there an 'and' equivalent to the 'or' operator?

I’m using regex in php to filter the list of data in a table on-the-fly as the user types into an input box. Say the user types “John” into the search box, this string gets sent to php via ajax, where a preg_match_all() loops through each row of data and returns only those rows where “John” is contained at least once. These positive matches then repopulate the table.

If the user however types “John Smith”, I want the preg_match_all() to loop through and return those rows where both “John” AND “Smith” are present (in any cell and any order). Similar functionality to the search box in iTunes etc.

Anyway I have discovered this is trickier than it sounds. It is easy to search for one OR the other (e.g. /string1|string2/i ) but this expands my list of returned items rather than filtering it down further as I want. Surely there must be a simple way to search for “string1” AND “string2” (AND string3 AND string4 etc as necessary), and where strings can appear in any order, using regex?

Any help would be really appreciated!

That sounds like you need to use OR

“John Smith” OR “Smith John” etc.

A single character can’t contain two values at the same time. The first letter of a string can’t be ‘A’ AND ‘B’, it can only be ‘A’ OR ‘B’. That’s why AND is unnecessary since it is impossible that a character can have two values at the same time.

Hi Stephen and thanks for the reply.

While on a technical level I understand that a character can’t contain two values at the same time, unfortunately your suggestion still doesn’t help me solve my problem. I’ll use another example to clarify what I’m trying to do:

Say we have:

$str1 = “The boy jumped over the creek”;
$str2 = “The girl jumped over the creek”;
$str3 = “The boy flew over the creek”;

If user1 wants to know which strings contain both “creek” and “boy” (and for the sake of the example they type them in that order), we want to return a positive match for $str1 and $str3.

If user2 wants to know which strings contain “jumped”, “boy”, and “creek”, we want to return a positive match just for $str1. User3 types “the”, “over” and “creek” and gets a positive match for all three strings.

Your suggestion (e.g. for user1: “creek boy” OR “boy creek”) would only return a match if “boy” and “creek” were directly next to each other, and so would not match positive to any of the above strings. Additionally, the potential combinations that would need to be covered could become endless as the user types more search terms.

Apologies if I ended that last reply rather abruptly, was running out the door as I typed it. Anyway I’m wondering if the only solution is to use arrays? I.e. put each string I need to search in an array, and foreach of these strings loop through a search term array, testing whether each search term appears in the string. Then return the string only if there is a positive match for each search term.

If there’s a simpler way though I’d love to hear it.

For your first one the regular expression would be:


which tests for those two words appearing anywhere in the string in either order.