1

I am working on a small project that sends SMS Messages. I already have the message classes written, and ready to send back in the correct format.

However, I am having a technical design issue. I'm sure it is easily solved, but I haven't come across this issue before so I am lost in the dark.

When I return a message longer than 160 chars, it obviously needs broken down into smaller messages and sent individually. My first thoughts are obviously to get the string length, then divide it by 160 chars, and round up to the greatest whole number (since you cannot send 2.5 messages,only 3) and then each sms needs to have 1/3, 2/3, 3/3 etc in the message as well showing the user which order to read them in. (trust me, they will need it)

My thoughts are obviously using some kind of loop to create a 'new ServiceMessage()' for each 160 char message. But I am unsure exactly how to do it, as well as unsure how to show the count of message (1/2, 2/3, etc).

Fairly simple, I am sure, but this is my first shot at it - so any help is greatly appreciated! Thank you!

4
  • It seems you've answered your own question! I would dive by 160 and round up to get number of messages. Then loop each 160 characters and create a new message object and indicate it as message 1 and then increment counter and continue...
    – Abs
    Jul 14, 2011 at 22:18
  • 1
    Agree with Abs but divide by something like 145-150 so that you have space to make sure words are not split across messages and you have space for the count number. Jul 14, 2011 at 22:21
  • How uncommon is it for SMS gateways not to split the message for you?
    – Scuzzy
    Jul 14, 2011 at 23:18
  • I was surprised that the gateway didn't do it automatically as well. I will be working with several others in the near future and will definitely keep a note of which ones do/don't Jul 15, 2011 at 0:47

4 Answers 4

7
$messages = str_split($message , 160);
foreach($messages as $message){
   // send $message
}

A more complicated version with the number of message itself can be like this, the code is untested:

if(strlen($message) > 160){
    /// lets use 152 characters and keep room for message number like (1/10), 
    /// we can have upto 99 parts of the message (99/99)

    $messages = str_split($message , 152); 
    $how_many = count($messages);
    foreach($messages as $index => $message){
        $msg_number = ($index + 1);
        $message = "(".$msg_number."/".$how_many.") ".$message;

        // send $message
    }
}
else{
    // send $message
}

The above might waste 2 characters per message but it keeps the calculation rather simple.

0
0

I haven't tested this but something like:

$len = strlen($message);
$parts = ceil($len / 160);
$last = 0;

for ($i = 1; $i <= $parts; $i++) {

    echo "Sending part " . $i . " of " . $parts;

    // Extract the next 160 characters
    $message_part = substr($message, $last, 160);
    $last += 160;

    // Send message
    new ServiceMessage($message_part);
}
0
    $messageSize = strlen( $message );
    $segmentSize = 160

    for ( $segment = 0; $segment * $segmentSize < $messageSize; $segment++ ) {
       $header = ($segment + 1).'/'.($messageSize / 160 + 1).' ';
       $segments[$segment] = substring( $message, 
                                        $segment * $segmentSize,
                                        $segmentSize - strlen( $header ) );
    }

//Your messages will be in the array $messages at this point

Off the cuff - but this should be close.

0

Without knowing how the ServiceMessage class works, I can't really provide you perfect code, but this should be pretty close to what you need. This placed into a function will return a ServiceMessage object, or an array of ServiceMessages objects. From there it's as simple sending the message using what ever function you wish if the ServiceMessage class does not do so intrinsically.

function getServiceMessage($message, $maxLen = 160)
{
    if(strlen($message) < $maxLen)
        return new ServiceMessage($message);

    $messages = explode(PHP_EOL, wordwrap($message, $maxLen - 5, PHP_EOL, TRUE));
    for ($i = 0, $j = 1, $k = count($messages); $i <= $k; ++$i, ++$j)
        $messages[$i] = new ServiceMessage("($j/$k){$messages[$i]]}");

    return $messages;
}

It should be noted that this will work only with a message length less then 1600 charaters, if you need to send a message greater then then that, you will need to change the $maxLen - 5 to $maxLen - 7 in order to allow it to format the number part correctly. If you wish to send a message greater then 16000 then you will need to change it to $maxLen - 9, and so on for the orders of magnitude.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.