我正在尝试将一条长消息分成 140 个长部分。如果消息部分不是最后一个,我想在它的末尾添加 3 个点。
我在下面的 for 循环中遇到问题 - 取决于消息长度部分是否丢失,最后一条消息也添加了 3 个点:
$length = count($message);
for ($i = 0; $i <= $length; $i++) {
if ($i == $length) {
echo $message[$i];
} else {
echo $message[$i]."...";
}
}
这是完整的代码:
$themessage = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
function split_to_chunks($to,$text) {
$total_length = (137 - strlen($to));
$text_arr = explode(" ",$text);
$i=0;
$message[0]="";
foreach ($text_arr as $word) {
if ( strlen($message[$i] . $word . ' ') <= $total_length ) {
if ($text_arr[count($text_arr)-1] == $word) {
$message[$i] .= $word;
} else {
$message[$i] .= $word . ' ';
}
} else {
$i++;
if ($text_arr[count($text_arr)-1] == $word) {
$message[$i] = $word;
} else {
$message[$i] = $word . ' ';
}
}
}
$length = count($message);
for ($i = 0; $i <= $length; $i++) {
if($i == $length) {
echo $message[$i];
} else {
echo $message[$i]."...";
}
}
return $message;
}
if (strlen(utf8_decode($themessage))<141) {
echo "Send";
} else {
split_to_chunks("",$themessage);
}
代码有什么问题?