title: PHP - Split text into 140 character parts
body: <p>I'm trying to split a long message into 140 long parts. If the message part is not the last one, I want to add 3 dots to the end of it. </p>
  
  <p>I'm having problems with the for loop below - depending on the message length parts are missing and the last message gets 3 dots added too:</p>
  
  <pre><code>$length = count($message);
  
  for ($i = 0; $i &lt;= $length; $i++) {
      if ($i == $length) {
          echo $message[$i];
      } else {
          echo $message[$i]."...";
      }
  
  }
  </code></pre>
  
  <p>This is the complete code:</p>
  
  <pre><code>$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 . ' ') &lt;= $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 &lt;= $length; $i++) {
  
          if($i == $length) {
          echo $message[$i];
          } else {
          echo $message[$i]."...";
          }
  
      }
      return $message;
  }
  
  if (strlen(utf8_decode($themessage))&lt;141) {
      echo "Send";
  } else {
      split_to_chunks("",$themessage);
  }
  </code></pre>
  
  <p>What is wrong with the code?</p>