2

長いメッセージを 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);
}

コードの何が問題になっていますか?

4

4 に答える 4

4

chunk_splitで試してみてください

echo substr(chunk_split($themessage, 137, '...'), 0, -3);

完全な単語を保持するには、wordwrapを使用するだけです

echo wordwrap($themessage, 137, '...');
于 2013-07-29T20:08:51.800 に答える
0

を使用しstr_splitます。文字列と長さを渡すことができ、パーツの配列を返します。

最後の項目を解決するには、もう少しコードが必要だという Andy の意見は正しいです。つまり、本当にうまくやりたいのであれば。多くのコードでは、最後のチャンクの長さが 1、2、または 3 文字であっても、文字列を 137 文字に分割し、それぞれの後に「...」を追加するだけなので、最後から 2 番目の項目と結合できます。

とにかく、ここにコードがあります:

<?php
function chunkify($str, $chunkSize, $postfix)
{
  $postfixLength = strlen($postfix);
  $chunks = str_split($str, $chunkSize - $postfixLength);
  $lastChunk = count($chunks) - 1;
  if ($lastChunk > 0 && strlen($chunks[$lastChunk] <= $postfixLength))
  {
    $chunks[--$lastChunk] .= array_pop($chunks);
  }

  for ($i = 0; $i < $lastChunk; $i++)
  {
    $chunks[$i] .= '...';
  }

  return $chunks;
}

var_dump(
  chunkify(
    'abcdefghijklmnopqrstuvwxyz', 
    6, // Make this 140.
    '...'));
于 2013-07-29T20:09:01.940 に答える
-1

再帰はどうですか?

/**
 * Split a string into chunks
 * @return array(part, part, part, ...) The chunks of the message in an array
 */
function split_to_chunks($str) {
  // we're done
  if (strlen($str) <= 140) 
    return array($str);

  // otherwise recur by merging the first part with the result of the recursion
  return array_merge(array(substr($str, 0, 137).  "..."), 
                     split_to_chunks(substr($str, 137))); 
}

単語の境界で分割する場合は、チャンク内の最後の空白文字のインデックスを見つけます。

// splits on word boundaries
function split_to_chunks($str) {
  // we're done
  if (strlen($str) <= 140) 
    return array($str);

  $index = strrpos(substr($str, 0, 137), " ");
  if (!$index) $index = 137;
  return array_merge(array(substr($str, 0, $index).  "..."), 
                     split_to_chunks(substr($str, $index))); 
}
于 2013-07-29T20:08:37.073 に答える