foreach - PHP explode then double loop -
i trying split string stay under limit of 70 characters... however, when this, loop stop right when gets first 70 characters , doesn't attempt 2nd set. reason i'm going route , not use str_split
keep entire words don't send out messages half of word. if 2nd split has less 70 characters, please still send out...any kind of appreciated.
$message="a new powerful earthquake convulsed traumatized nation of nepal on tuesday, leveling buildings damaged devastating quake killed thousands of people less 3 weeks ago." $msg = explode(' ',$message); foreach($msg $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; } }
<?php $message = "a new powerful earthquake convulsed traumatized nation of nepal on tuesday, leveling buildings damaged devastating quake killed thousands of people less 3 weeks ago."; define ("max_packet_size", 70); $msg = explode (' ',$message); $indexes = array (0); $actualsize = 0 ; ($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); ($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(); ($i=$start ; $i<$stop ; $i++) { $temp[] = $array[$i]; } return $temp; }
Comments
Post a Comment