Search code examples
phpsplitchunkssentence

PHP - Split String Into Arrays for every N characters


Is it possible to take a very long string and split it by sentence into 5000 char (or smaller) array items?

Here's what I have so far:

<?php
$text = 'VERY LONG STRING';

foreach(explode('. ', $text) as $chunk) {
    $accepted[] = $chunk;
}
?>

This just splits the string into an array containing single sentence items. I need to group items into sub arrays, each containing a list of items which, when added together, contain no more than 5000 characters.

I tried this:

<?php
$text = 'VERY LONG STRING';

foreach(explode('. ', $text) as $chunk) {
    $key = strlen(implode('. ', $accepted).'. '.$chunk) / 5000;
    $accepted[$key][] = $chunk;
}
?>

You can probably see what I tried to do here, but it didn't work.

UPDATE: This did the trick:

<?php
foreach(explode('. ', $text) as $chunk) {
  $chunkLen = strlen(implode('. ', $result).'. '.$chunk.'.');
  if ($len + $chunkLen > 5000) {
    $result[] = $partial;
    $partial = [];
    $len = 0;
  }
  $len += $chunkLen;
  $partial[] = $chunk;
}

if($partial) $result[] = $partial;
?>

Solution

  • You could do something like this:

    $text = 'VERY LONG STRING';
    $result = [];
    $partial = [];
    $len = 0;
    
    foreach(explode(' ', $text) as $chunk) {
      $chunkLen = strlen($chunk);
      if ($len + $chunkLen > 5000) {
        $result[] = $partial;
        $partial = [];
        $len = 0;
      }
      $len += $chunkLen;
      $partial[] = $chunk;
    }
    
    if ($partial) {
        $result[] = $partial;
    }
    

    You can test it more easily if you do it with a lower max length