長いメッセージを 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);
}
コードの何が問題になっていますか?