Preg_match needs to search for a word, not any part of a word that matches
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))) {
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.
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.phperror_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";}
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.phperror_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 ";}
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><?phpinclude '../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>
<?php$From = $_REQUEST['From'];$body = $_REQUEST['Body'];header("content-type: text/xml");echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";?><Response><?phpinclude '../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>
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><?phpinclude '../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>
try that. The carat ^ means not matching.