3

I am trying to split up a string to stay under the limit of 70 characters... however, when i do this, my loop just stop right when it gets the first 70 characters and it doesn't attempt to do the 2nd set. The reason I'm going this route and not use str_split is to keep the entire words so I don't send out messages with half of a word. If the 2nd split has less than 70 characters, please still send it out...any kind of help with this is greatly appreciated.

$message="A new powerful earthquake convulsed the traumatized nation of Nepal on Tuesday, leveling buildings already damaged by the devastating quake that killed thousands of people less than three weeks ago."

$msg = explode(' ',$message);

 foreach($msg as $key) {    

    $keylen = strlen($key);
    $msglen = $msglen + $keylen;
    if($msglen<70) {
    $msgs .=$key." ";
    // $agi->verbose("$msgs");
    } else {    
    $params = array(
            'src' => '18009993355',
            'dst' => $callerid,
            'text' => $msgs,
            'type' => 'sms',
        );
    // $agi->verbose("sending: $msgs");
    $response = $p->send_message($params);
    $msgs = "";
    $msglen = 0;
    }
 }
5
  • Can you please update your question along with string Commented May 12, 2015 at 8:47
  • we can't do much without $message, I'm sorry. Commented May 12, 2015 at 8:50
  • Why is it that you are saying 70 char and comparing to $msg<50 ??? Commented May 12, 2015 at 8:53
  • As I see, if $message has less than 50 characters it will never be sent Commented May 12, 2015 at 8:53
  • added the $message.. if the 2nd message has 30 characters... still send it out.. just not to send messages out that has more than 70 characters... Commented May 12, 2015 at 9:06

2 Answers 2

2
<?php
$message = "A new powerful earthquake convulsed the traumatized nation of Nepal on Tuesday, leveling buildings already damaged by the devastating quake that killed thousands of people less than three weeks ago.";

define ("MAX_PACKET_SIZE", 70);

$msg        = explode (' ',$message);
$indexes    = array (0);
$actualSize = 0 ;
for ($i=0 ; $i<count($msg) ; $i++) {
    if ($actualSize + strlen ($msg[$i]) <= MAX_PACKET_SIZE ) {
        $actualSize += strlen ($msg[$i]);
        if (($i+1) < count($msg)) {
            $actualSize++;
        }
    }else {
        $indexes[]  = $i;
        $actualSize = 0 ;
    }
}
$indexes[] = count ($msg);


for ($i=1 ; $i<count($indexes) ; $i++) {
    $temp = array_extract ($msg, $indexes[$i-1], $indexes[$i]);
    var_dump(implode (' ', $temp));
    $params = array ('src'  => '18009993355',
                     'dst'  => $callerid,
                     'text' => implode (' ', $temp) ,
                     'type' => 'sms');
    // $agi->verbose("sending: $msgs");
    $response = $p->send_message($params);
}

function array_extract ($array, $start, $stop) {
    $temp = array();
    for ($i=$start ; $i<$stop ; $i++) {
        $temp[] = $array[$i];
    }
    return $temp;
}
Sign up to request clarification or add additional context in comments.

4 Comments

this works, but it doesn't send the remaining messages... it only send out the firs tone.
you did not specify it in your spec :) update your question and i will update my response
something is wrong with this, I get 4 messages of each response...then 12 messages of the last response...
@thevoipman i have completely changed my approach and i tested it :)
0

What does $msg contain? If the first message contains 49 or less characters, whilst the second message contains another 50 characters, it will not send the second message, isn't that the point?

You could place some var_dumps here and there to debug it.

1 Comment

and where is your response ? please set it as a comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.