4

I use the "OR" operator "|" to match one of the words of the $name variable:

$name = "one five six two";
if (preg_match('/(one|two)/i', $name)) {return true;}

What operator should I use with preg_match to have an "AND" condition if those words are inside of $name?

For example,

if (preg_match('/(two "AND" five)/i', $name)) {return true;}

5 Answers 5

4

If you still want to use regex, you'll need positive lookaheads:

if (preg_match('/^(?=.*one)(?=.*two)/i', $name)) {return true;}

It's not recommended for simple stuff (kind of overkill), and it gets messy with more complex stuff...

Sign up to request clarification or add additional context in comments.

9 Comments

Can I use single preg_match function to check like: [anything beging]two[anything middle]five[anything end] ?
@KenTang Isn't that a single preg_match? Also, it looks for one and two anywhere in $name.
Does complex single function (?=.*one)(?=.*two) can uses more CPU resources and calculation time compare to double preg_match use?
@KenTang If $name is very long, yes. Otherwise, the difference shouldn't be too noticeable.
@patrick: Jerry, along with all the other responders, interpreted the question as how to match two particular words in any order. That requires two lookaheads. So you weren't simplifying the answer, you were changing it.
|
3

I think you just need to separate the two conditions and use && as follows:

if(preg_match('/(two)/i', $name) && preg_match('/(five)/i', $name)) {
    return true;
}

Learn more here.

1 Comment

Can I use single preg_match function to check like: [anything]two[anything]five[anything] ?
1

You might just do this without resorting to regular expressions:

if (strpos($name, 'one') !== false && strpos($name, 'two') !== false) {
   // do something
}

1 Comment

Perhaps explain the equal false parts in your answer? (The documentation says "Use the === operator for testing the return value of this function.") - not the negation, but why it can't just be strpos($name, 'one') without !== false. (But without "Edit:", "Update:", or other meta talk - the answer should appear as if it was written today)
0
if ( preg_match('/two/i', $name) && preg_match('/five/i', $name) ) {return true;}

3 Comments

Can I use single preg_match function to check like: [anything]two[anything]five[anything] ?
Yes. Regular expressions can seem daunting at first, but if you spend a little time learning the basics it will pay off a lot in the long run. To answer the question in your comment: preg_match('/.*two.*five.*/i', $name) or if you want to match the entire string $name with this pattern then preg_match('/^.*two.*five.*$/i', $name)
The last comment seems to contradict Jerry's answer.
-2

Don't use preg_match if you only match one word. A regular expression uses more computer resources compared to strpos.

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster (from php.net).

if (str_pos($name, 'two') !== false
    && str_pos($name, 'five') !== false) {

    return true;
}

1 Comment

I wasn't the one to downvote, but matching one word with regexp can be VERY useful. Your example matches too when $name='fleetwood' for instance... regexp( '/\btwo\b/i', $name ) would not. Adding spaces to your $name and then doing a search would miss 'one, two, three', 'cause there is an ' attached to the two. Again, the regexp would be the best solution in this case... "matching one word" is NOT the same as "one string is contained in another"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.