hadoob024 Posted May 14, 2008 Share Posted May 14, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/105637-splitting-up-text-into-chunks-no-split-words/ Share on other sites More sharing options...
StormTheGates Posted May 14, 2008 Share Posted May 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105637-splitting-up-text-into-chunks-no-split-words/#findComment-541216 Share on other sites More sharing options...
moselkady Posted May 14, 2008 Share Posted May 14, 2008 Could this work for you? echo wordwrap ($body,130); Quote Link to comment https://forums.phpfreaks.com/topic/105637-splitting-up-text-into-chunks-no-split-words/#findComment-541218 Share on other sites More sharing options...
hadoob024 Posted May 14, 2008 Author Share Posted May 14, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/105637-splitting-up-text-into-chunks-no-split-words/#findComment-541232 Share on other sites More sharing options...
hadoob024 Posted May 14, 2008 Author Share Posted May 14, 2008 Just figured it out. The following line: $tmp_body = substr($tmp_body, $tmp_start, 130 + $inc); needed to be this instead: $tmp_body = substr($tmp_body, 1, 130 + $inc); Quote Link to comment https://forums.phpfreaks.com/topic/105637-splitting-up-text-into-chunks-no-split-words/#findComment-541239 Share on other sites More sharing options...
effigy Posted May 14, 2008 Share Posted May 14, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105637-splitting-up-text-into-chunks-no-split-words/#findComment-541244 Share on other sites More sharing options...
moselkady Posted May 14, 2008 Share Posted May 14, 2008 You can split it into array: $array = split("\n", wordwrap($body,130)); Quote Link to comment https://forums.phpfreaks.com/topic/105637-splitting-up-text-into-chunks-no-split-words/#findComment-541245 Share on other sites More sharing options...
hadoob024 Posted May 14, 2008 Author Share Posted May 14, 2008 Sweet. Both of those work too. Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/105637-splitting-up-text-into-chunks-no-split-words/#findComment-541277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.