Find "New World Order" in sentence, PHP REGEX

I would like to have the following done:
Use php "preg_replace" and regex only.

Find the following in the string.

$string = "The president traveled across the world in his new plane in order to go to the conference"

You'll notice that the above string has "new" "world" "order", but not in that particular order. I would like it to return a positive if those three words are found in any sequence if possible.
Who is Participating?
strpos($str, ' new ')   &&
     strpos($str, ' world ') &&
     strpos($str, ' order ')
fails on
"new world order"
You might have to first pad it to
" new world order "
Just a tip for the future... check out The Regex Coach here if you find yourself working with regular expressions. It's a great program.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Ray PaseurCommented:
"Use php "preg_replace" and regex only."

Please see the prominent note on the PHP man page for preg_match, that says, "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."

Is this a school assignment?  If it is, you need to be aware that the terms of service for EE prohibit us from doing school assignments.  We are allowed to advise and suggest learning resources, that is all.

If it is not a school assignment, then you probably want to follow the advice on PHP.NET and skip the REGEX and use the strpos() function to find those three items.  Or perhaps explode() and in_array().  Any of those will be easier to get right than the REGEX.  You can install this and test it to see if it works right for you.

A good REGEX learning tool is available in this post from

best regards, ~Ray

<?php // RAY_temp_new_world_order.php

$str = "The president traveled across the world in his new plane in order to go to the conference";

if ( strpos($str, 'new')   &&
     strpos($str, 'world') &&
     strpos($str, 'order') )
    echo "FOUND THEM";

Open in new window

strpos may match on "crossworders unworldly stoneware"
www_puertoricoautoforo_comAuthor Commented:
I need it using "preg_replace"  even though it is just one thing...

No, it's not a school assignment, it's a supplement to the original script you wrote me (thanks ray) , that's why I need it to work with preg_replace, because I have a ton of other patterns to also check for using preg_replace.

Ray PaseurCommented:
@ozo: Yes, if you are too literal, you can make it fail.  Or a creative individual might be able to do something like this code snippet (and that is partially why I suggested in_array() - you could make an array of the things you wanted to look for and another array of the things you wanted to avoid.

There are many other considerations that may come into play, too, since we do not have all the other test strings.  For instance, there may be (or may not be) punctuation, case-sensitivity, adjacent numbers, compound words, etc.  I find this kind of word-parsing always requires iterative steps to define the objectives clearly.  It is almost never right the first time, whether you use REGEX or some other technique.  The problem definition is the central issue.


if ( strpos($str, ' new ')   &&
     strpos($str, ' world ') &&
     strpos($str, ' order ') )

Open in new window

Ray PaseurCommented:
Right, that is the "creative individual" part.  You might also want to run it through strtoupper and test with only uppercase search strings or use stripos - those are all business rules that the programmer must deal with.  As I said, these kind of string parsing problems never get solved the first time.  What should we do about the "New World Symphony" vs "Neworld Productions" - just lots of things to think about.

Best regards, ~Ray
www_puertoricoautoforo_comAuthor Commented:
I was not able to use these in my preg_replace, but thanks for help anyways
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.