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

array_slice如果有超过 140 个键,则使用,否则按原样打印

<?php
$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.";
$words = explode(' ', $themessage);

if (count($words) > 140)
{
    echo implode(' ', array_slice($words, 0, 140)) . '...';
}
else
{
    echo $themessage;
}
?>

除非您想要 140 个字符,而不是单词 - 您的示例没有明确定义,但您的代码提供了两个选项。为了那个原因:

<?php
$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.";
if (strlen($themessage) > 140)
{
    echo preg_replace('/\s*[^\s]+$/', '...', substr($themessage, 0, 137));
}
else
{
    echo $themessage;
}
?>
于 2013-07-29T20:05:30.173 回答
0

使用str_split. 它允许您传递一个字符串和一个长度,并返回一个部分数组。

Andy 是对的,您需要更多代码来解决最后一项。也就是说,如果你想做它真的很好。许多代码只会将字符串拆分为 137 个字符并在每个字符后添加“...”,即使最后一个块的长度为 1、2 或 3 个字符,所以它可以与倒数第二个项目连接。

无论如何,这是代码:

<?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 回答