PHP - 将文本拆分为140个字符的部分

时间:2013-07-29 19:58:41

标签: php

我正在尝试将长信息分成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 个答案:

答案 0 :(得分:4)

使用chunk_split

进行尝试
echo substr(chunk_split($themessage, 137, '...'), 0, -3);

要保留完整的字词,请使用wordwrap

echo wordwrap($themessage, 137, '...');

答案 1 :(得分:0)

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

<?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;
}
?>

答案 2 :(得分: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.
    '...'));

答案 3 :(得分:-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))); 
}