我使用的是一个糟糕的PDFLib包装器,它不能处理PDFLib处理超过字符限制(每个单元大约1600个字符)的单元格的问题。因此,我需要将一个大段落拆分成较小的字符串,这些字符串整齐地适合单元格,而不是拆分单词,并且尽可能靠近行尾。我完全不知道如何有效地做到这一点(我需要它在合理的时间内运行)
以下是我的代码,它将块分割为仅基于字符长度的子字符串,忽略了我上面提到的单词和行要求:
SPE_*函数是来自包装器类的静态函数,SetNextCellStyle调用用于在单元格的轮廓周围绘制一个框,BeginRow是开始一行文本所必需的。EndRow用于结束一行文本,必须在BeginRow之后调用,如果预设的列数未填满,则会产生错误。AddCell将该字符串添加到第二个参数列数中。
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++;
}
}提前感谢您的帮助!
发布于 2012-06-26 01:34:12
像这样的东西应该行得通。
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)https://stackoverflow.com/questions/11194231
复制相似问题