AtomicRax Posted April 16, 2011 Share Posted April 16, 2011 I'm working on a PHP SMS gateway of sorts. I have the txt message saved to the variable $message but I have a small problem with multi-message text messages.. When most phones send multiple messages as one, they include a (X/Y) before the message.. X indicating which message it is out of Y messages. A rough example: the first message comes through: $message = "(1/2) This is a multimessage text message that"; the second message comes through: $message = "(2/2) will use more than one message to send"; I would like to be able to parse these... as such: if ($message == "(1/Y)") { // Save first message } elseif ($message == "(2+/Y)") { // Append to original message // This number can be anything 2 and above } else { // One message text, so save it like normal } If I can figure this out, I'll write a tutorial in a few days on how to set up a free php sms gateway for your own project! Including how/where to get a free number to use! Quote Link to comment https://forums.phpfreaks.com/topic/233874-sms-multi-message-parsing/ Share on other sites More sharing options...
AtomicRax Posted April 16, 2011 Author Share Posted April 16, 2011 Can't modify my post anymore.. I do it too much!! oh well.. I have thought about that if I can just figure out if the message starts with (#/#) in the beginning, I can go from there.. which would probably be a regex problem.. hmmm Quote Link to comment https://forums.phpfreaks.com/topic/233874-sms-multi-message-parsing/#findComment-1202251 Share on other sites More sharing options...
maxudaskin Posted April 16, 2011 Share Posted April 16, 2011 First off, I would suggest setting an empty value to $message before your if/else statements. <?php $message = ''; Then you have to find the part of the string (x/y). Use preg_match. <?php $message = ''; $pattern = "/^\([0-9]+\/[0-9+]\)/"; if(preg_match($pattern, $sms)) { // It is a multi-part message } else { // It is not a multi-part message } Then your going to want to collect all of the messages, then sort them. I'll let you figure that out, but to append one after another is as simple as doing this. <?php $message = ''; $pattern = "/^\([0-9]+\/[0-9+]\)/"; if(preg_match($pattern, $sms)) { // It is a multi-part message $message .= $message_part; // You will have to loop to get it to continually append to the end of the string. } else { // It is not a multi-part message $message = $message_part; } Quote Link to comment https://forums.phpfreaks.com/topic/233874-sms-multi-message-parsing/#findComment-1202390 Share on other sites More sharing options...
AtomicRax Posted April 18, 2011 Author Share Posted April 18, 2011 Thanks maxudaskin! As promised, I have written a tutorial that is now available at http://theyconfuse.me/about/an-sms-gateway on how to create a free basic SMS gateway to receive and log SMS messages to a MySQL database! Quote Link to comment https://forums.phpfreaks.com/topic/233874-sms-multi-message-parsing/#findComment-1202842 Share on other sites More sharing options...
maxudaskin Posted April 18, 2011 Share Posted April 18, 2011 Looking at it now, I think that my pattern string is erroneous, but I guess you got that figured out Thanks for the tutorial, it's pretty cool, although, I cannot access any of those features for Google Voice with my account. I assume that it's because I live in Canada. Quote Link to comment https://forums.phpfreaks.com/topic/233874-sms-multi-message-parsing/#findComment-1203191 Share on other sites More sharing options...
AtomicRax Posted April 19, 2011 Author Share Posted April 19, 2011 Well, what I have in the tutorial worked for a few tests, so I left that alone.. The tutorial was the basic functionality.. I have more coding in my actual project. Sorry to hear about the lack of Google Voice features! I think you're correct in assuming it's because you live in Canada. I believe Google Voice charges to send text messages to another country; I'm not sure if they charge to receive any (though your carrier might charge extra to send them!).. You could try to use a US proxy to set up your Google Voice account? I tried, man! Quote Link to comment https://forums.phpfreaks.com/topic/233874-sms-multi-message-parsing/#findComment-1203294 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.