PHP 爆炸然后双循环
PHP explode then double loop
我正在尝试拆分一个字符串以保持在 70 个字符的限制内...但是,当我这样做时,我的循环只是在它获得前 70 个字符时立即停止并且它不会尝试做第二组。我走这条路而不使用 str_split
的原因是为了保留整个单词,这样我就不会发送半个单词的消息。如果第 2 部分少于 70 个字符,请仍然发送...非常感谢任何类型的帮助。
$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;
}
}
$msg 包含什么?如果第一条消息包含 49 个或更少的字符,而第二条消息包含另外 50 个字符,则不会发送第二条消息,这不是重点吗?
你可以在这里和那里放置一些 var_dumps 来调试它。
<?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;
}
我正在尝试拆分一个字符串以保持在 70 个字符的限制内...但是,当我这样做时,我的循环只是在它获得前 70 个字符时立即停止并且它不会尝试做第二组。我走这条路而不使用 str_split
的原因是为了保留整个单词,这样我就不会发送半个单词的消息。如果第 2 部分少于 70 个字符,请仍然发送...非常感谢任何类型的帮助。
$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;
}
}
$msg 包含什么?如果第一条消息包含 49 个或更少的字符,而第二条消息包含另外 50 个字符,则不会发送第二条消息,这不是重点吗?
你可以在这里和那里放置一些 var_dumps 来调试它。
<?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;
}