Solved

Preg_match needs to search for a word, not any part of a word that matches

Posted on 2011-09-11
11
331 Views
Last Modified: 2012-05-12
Hi,
Back at the trough of knowledge again...

I'm using preg_match to detect if someone text messages the word 'add' or 'subscribe' in a text message so I know if they want to be subscribed to my app.

if ((preg_match("/add/i", $body)) || (preg_match("/subscribe/i", $body))) {

Open in new window



Problem I've just noticed is I also use:

else if ((preg_match("/remove/i", $body)) || (preg_match("/unsubscribe/i", $body))) {

Open in new window


to tell if they want to be removed. Preg_match is matching any string that contains those letters (case insensitive). Works great for 'add' and 'remove' but 'subscribe' and 'unsubscribe' is a problem. I'm guessing its seeing the 'subscribe' in unsubscribe first and acts accordingly.

What would I use to look for the words? I thought about a space at each end but someone could type out one of the words starting without a space or ending without.
0
Comment
Question by:tjyoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 10

Expert Comment

by:Derokorian
ID: 36519740
if ((preg_match("/add/i", $body)) || (preg_match("/[^un]subscribe/i", $body))) {

try that. The carat ^ means not matching.
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 36519743
Actually it might need to be parentheses instead of brackets.

if ((preg_match("/add/i", $body)) || (preg_match("/(^un)subscribe/i", $body))) {
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36519747
I believe that you can use the \w character class to delimit words.

you might also find the (XXX) group operator to be useful.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 1

Author Comment

by:tjyoung
ID: 36519761
I don't think the carat method is doing it. Add and Remove are working as expected so I am guessing the other 2 are problems.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 167 total points
ID: 36519771
Sorry - not \w, but \b does the word-boundary identification.
http://www.laprbass.com/RAY_temp_tjyoung.php
outputs:
This is a request to add or subscribe MATCHES #(\bADD\b|\bSUBSCRIBE\b)#i
This is a request to unsubscribe or addle
SuBscrIBE MATCHES #(\bADD\b|\bSUBSCRIBE\b)#i
Add. MATCHES #(\bADD\b|\bSUBSCRIBE\b)#i
  Add.??! MATCHES #(\bADD\b|\bSUBSCRIBE\b)#i
AddSubscribe
Subscriber
unsubscribe
un-subscribe MATCHES #(\bADD\b|\bSUBSCRIBE\b)#i

Best regards, ~Ray
<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);
echo "<pre>";

// TEST STRINGS IN AN ARRAY
$arr = array
( 'This is a request to add or subscribe'
, 'This is a request to unsubscribe or addle'
, 'SuBscrIBE'
, 'Add.'
, '  Add.??!'
, 'AddSubscribe'
, 'Subscriber'
, 'unsubscribe'
, 'un-subscribe'
)
;

// A REGEX TO FIND THE ADD OR SUBSCRIBE
$rgx
= '#'                       // REGEX DELIMITER
. '(\bADD\b|\bSUBSCRIBE\b)' // WORD-DELIMITED "ADD" OR "SUBSCRIBE"
. '#'                       // REGEX DELIMITER
. 'i'                       // CASE-INSENSITIVE
;

foreach ($arr as $str)
{
    echo PHP_EOL . $str;
    if (preg_match($rgx, $str)) echo " MATCHES $rgx";
}

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36519788
Here is another variant.  Note the ambiguity of un-subscribe.  Might require some tinkering!

Outputs:
This is a request to add or subscribe WANTS TO ADD / SUBSCRIBE
This is a request to unsubscribe or addle WANTS TO REMOVE / UNSUB
SuBscrIBE WANTS TO ADD / SUBSCRIBE
Add. WANTS TO ADD / SUBSCRIBE
  Add.??! WANTS TO ADD / SUBSCRIBE
AddSubscribe
Subscriber
unsubscribe WANTS TO REMOVE / UNSUB
un-subscribe WANTS TO ADD / SUBSCRIBE  WANTS TO REMOVE / UNSUB
Please remove me from the list WANTS TO REMOVE / UNSUB
<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);
echo "<pre>";

// TEST STRINGS IN AN ARRAY
$arr = array
( 'This is a request to add or subscribe'
, 'This is a request to unsubscribe or addle'
, 'SuBscrIBE'
, 'Add.'
, '  Add.??!'
, 'AddSubscribe'
, 'Subscriber'
, 'unsubscribe'
, 'un-subscribe'
, 'Please remove me from the list'
)
;

// A REGEX TO FIND THE ADD OR SUBSCRIBE
$rgx_add
= '#'                           // REGEX DELIMITER
. '(\bADD\b|\bSUBSCRIBE\b)'     // WORD-DELIMITED "ADD" OR "SUBSCRIBE"
. '#'                           // REGEX DELIMITER
. 'i'                           // CASE-INSENSITIVE
;

// A REGEX TO FIND THE REMOVE OR UNSUB-SCRIBE
$rgx_rmv
= '#'                           // REGEX DELIMITER
. '(\bREMOVE\b|\bUN.*?SUBS*?)' // WORD-DELIMITED "REMOVE" OR VARIANT OF "UN-SUBSCRIBE"
. '#'                           // REGEX DELIMITER
. 'i'                           // CASE-INSENSITIVE
;

foreach ($arr as $str)
{
    echo PHP_EOL . $str;
    if (preg_match($rgx_add, $str)) echo " WANTS TO ADD / SUBSCRIBE ";
    if (preg_match($rgx_rmv, $str)) echo " WANTS TO REMOVE / UNSUB ";
}

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 36519844
Hi Ray,
I'm sure its right in principle but with my limited skillset, I can't seem to implement it into what I'm doing. I've embedded a sample out of desperation after many attempts.

This is the basic idea (I've omitted the db portions etc.)

 
<?php

$From = $_REQUEST['From'];
$body = $_REQUEST['Body'];

header("content-type: text/xml");
echo  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<Response>

<?php

include '../config.php';

	
if ((preg_match("/add/i", $body)) || (preg_match("/subscribe/i", $body))) {
	

}


 else if ((preg_match("/remove/i", $body)) || (preg_match("/unsubscribe/i", $body))) {

	


} else {?>
		<Sms>We're sorry. Our system did not understand your message. Please contact our station if you have any questions or concerns. Thank you.</Sms>
	<?php } ?>
	
</Response>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36519858
Click here and look at the output.
http://www.laprbass.com/RAY_temp_tjyoung.php

You might want to copy the script at ID:36519788 and adapt it to use your test data instead of my test data.
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 166 total points
ID: 36519873
try:
<?php

$From = $_REQUEST['From'];
$body = $_REQUEST['Body'];

header("content-type: text/xml");
echo  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<Response>

<?php

include '../config.php';

$temp=str_replace('-','',strtolower($body));	
if ( preg_match('/\b(remove|unsubscribe)\b/', $temp) ) {

	

}
elseif ( preg_match('/(add|subscribe)/', $temp) ) {

	


} else {?>
		<Sms>We're sorry. Our system did not understand your message. Please contact our station if you have any questions or concerns. Thank you.</Sms>
	<?php } ?>
	
</Response>

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 36519990
Hi heilo,
add, subscribe and remove work but when you send unsubscribe, it thinks it is 'subscribe'.
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 167 total points
ID: 36520013
Ray's idea of using word boundaries should do the trick (have altered hielo's code):
<?php

$From = $_REQUEST['From'];
$body = $_REQUEST['Body'];

header("content-type: text/xml");
echo  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<Response>

<?php

include '../config.php';

$temp=str_replace('-','',strtolower($body));	
if ( preg_match('/\b(remove|unsubscribe)\b/', $temp) ) {

	

}
elseif ( preg_match('/\b(add|subscribe)\b/', $temp) ) {  # CHANGED THIS LINE

	


} else {?>
		<Sms>We're sorry. Our system did not understand your message. Please contact our station if you have any questions or concerns. Thank you.</Sms>
	<?php } ?>
	
</Response>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question