Using preg_match to determine intent of message

Hello,
I have incoming SMS messages in my app. To subscribe to the SMS service you would type the word 'Go' and you are subscribed. The word 'Stop' and you are removed. I've also added a couple of additional words such as 'Add' or 'Subscribe' just in case they don't send 'Go' for instance.

What I'm wondering is, the below preg_match... it was provided to me but I'm thinking it checks the entire content of the incoming SMS to see if any of the words exist. I'd like to revise so it checks if one of the 3 words exist below... and its the only word in the entire message.

Any ideas? What I'm trying to accomplish is I may also receive regular correspondence via SMS so I want to treat those differently and not as a subscribe or unsubscribe request.


$temp=str_replace('-','',strtolower($body));      

if ( preg_match('/\b(add|subscribe|go)\b/', $temp) ) {
LVL 1
tjyoungAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dave4dlCommented:
you could use this instead:
if ( preg_match('/^(add|subscribe|go)$/', $temp) ) {

Open in new window


The "^" means the beginning of the line and the "$" means the end of the line (instead of \b which means any word boundary)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eriksmtkaCommented:
replace the existing "preg_match" with this:

preg_match('/^(add|subscribe|go)$/', $temp)

Open in new window


what i've done is, replace the first \b with an "^" and the last "\b" with a "$".

the \b matches a "word boundary", so it will match the word, pretty much anywhere.  the "^" and the "$" match the "beginning" and the "end" of the string, respectively.  so the match reads in real english: 'match the beginning of the string, then "add", "subscribe" or "go" then the end of the string'

that will effectively "throw out" everything else...  hope you don't want "misspellings" of those words... or at the very least have some way of catching them some other way!
0
eriksmtkaCommented:
You might also want to learn regular expressions.  There is an excellent website: Regex Tutorial

I first found that site years ago, and would always give up on it because it was "complicated".  I finally just got my arms around it.  Eventually it will "click".  Trust me, it's worth the time.  Things are far easier for me now that I'm able to use those!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.