Jump to content

Splitting up text into chunks (no split words)


hadoob024

Recommended Posts

I'm trying to figure out the best way to split up a body of text (say maybe 400-500 characters long) into 130 character-sized chunks with no words being split. I figured how to do this for the first chunk, but am having problems figuring out how to keep repeating it.  Been playing around with this for a while, and came up with the following, but can't seem to figure out why it's not working. Any thoughts?

 

$inc = -1;
$body_length = 0;
$tmp_chunk = 1;
$tmp_body = '';
$body = '';
$tmp_start = 1;
$final_chunk = 0;

$body = 'On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look.';

$body_length = length($body);
                  
do
  if (($tmp_start + 130) > $body_length) {
    $tmp_body = substr($body, $tmp_start, $body_length - $tmp_start);
    $final_chunk = 1;}
  else {
    $tmp_body = substr($body, $tmp_start, 130);}
  
           
  while (substr(tmp_body, inc, 1) != ' ')
      $inc = $inc - 1;
  endwhile;
         
  $tmp_body = substr($tmp_body, $tmp_start, 130 + $inc);
  $tmp_start = $tmp_start + 130 + $inc;
  
  echo $tmp_body;
  
  $tmp_chunk = $tmp_chunk + 1;
  $inc = -1;
  $tmp_body = NULL;
           

while ($final_chunk = 0);

Link to comment
Share on other sites

Well if you want to make sure that words are not split then I would say perhaps checking the character before and after your 130 mark to see if either is equal to a space? If not then read over one more until one of the surrounding chars equals a space, that will indicate the word has ended. You could also check for commas or colors or periods or whatever.

Link to comment
Share on other sites

Cool.  Thanks.  Yeah, the words can't be split.  Basically, I'm taking a chunk of text, and sending out SMS's in 130 character chunks.  Words can't be split in the middle, and a chunk can't be larger than 130 characters.

 

I just looked at wordwrap().  Quick follow-up though.  Instead of just echo-ing out each chunk, I need to store each chunk into a variable.  Wordwrap() doesn't seem to allow this, does it?

Link to comment
Share on other sites

Another option:

 

<pre>
<?php

$body = 'On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look.';
preg_match_all('/\S+/', $body, $matches, PREG_OFFSET_CAPTURE);

$chunks = array();
foreach ($matches[0] as $match) {
	list($word, $offset) = $match;
	$word_len = strlen($word);
	### +2: 1 to account for added space, 1 to up the 0 indexing.
	$chunks[(int)(($offset + $word_len + 2) / 130)] .= $word . ' ';
}

foreach ($chunks as $chunk) {
	printf('%3s: %s<br>', strlen($chunk), $chunk);
}

?>
</pre>

 

127: On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can

128: use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create

85: pictures, charts, or diagrams, they also coordinate with your current document look.

 

Edit: I updated the code, but you'll want to use wordwrap to handle very long words. I forgot about it :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.