Jump to content

SMS Multi-Message Parsing


AtomicRax

Recommended Posts

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!

 

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.