• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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) ) {
0
tjyoung
Asked:
tjyoung
  • 2
2 Solutions
 
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
 
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
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now