Question

I am using a terrible wrapper of PDFLib that doesn't handle the problem PDFLib has with cells that are more than the character limit (Which is around 1600 characters per cell). So I need to break a large paragraph into smaller strings that fit neatly into the cells, without breaking up words, and as close to the end of the line as possible. I am completely stumped about how to do this efficiently (I need it to run in a reasonable amount of time)

Here is my code, which cuts the block up into substrings based on character length alone, ignoring the word and line requirements I stated above:

SPE_* functions are static functions from the wrapper class, SetNextCellStyle calls are used to draw a box around the outline of the cells BeginRow is required to start a row of text. EndRow is required to end a row of text, it must be called after BeginRow, and if the preset number of columns is not completely filled, an error is generated. AddCell adds the string to the second parameter number of columns.

function SPE_divideText($string,$cols,$indent,$showBorders=false)
{
$strLim = 1500;
$index = 0;
$maxIndex = round((strlen($string) / 1500-.5));
$retArr= array();
  while(substr($string, $strLim -1500,$strLim)!=FALSE)
    {
    $retArr[$index] = substr($string, $strLim -1500,$strLim);
            $strLim+=1500;

            SPE_BeginRow();
            SPE_SetNextCellStyle('cell-padding', '0');
            if($indent>0)
              {

              SPE_Empty($indent);
              }
            if($showBorders)
              {
            SPE_SetNextCellStyle('border-left','1.5');
            SPE_SetNextCellStyle('border-right','1.5');

              if($index == 0)
                {
                SPE_SetNextCellStyle('border-top','1.5');
                }
                if($index== $maxIndex)
                  {
               SPE_SetNextCellStyle('border-bottom','1.5');

                  }
              }

            SPE_AddCell($retArr[$index],$cols-$indent);
            SPE_EndRow();
            $index++;
    }
}

Thanks in advance for any help!

Was it helpful?

Solution

Something like this should work.

function substr_at_word_boundary($string, $chars = 100)
{
    preg_match('/^.{0,' . $chars. '}(?:.*?)\b/iu', $string, $matches);
    $new_string = $matches[0];
    return ($new_string === $string) ? $string : $new_string;
}

$string = substr_at_word_boundary($string, 1600)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top