我有一个PHP 函数,输入两个变量,分别为宽度和文本串。它会计算根据输入指定的宽度,该文本串可以分成多少行显示。这个函数能支持英文文本串,为啥输入中文就会报编码错误呢 NbLines Malformed UTF-8 characters, possibly incorrectly encoded 。 如何修改能让它 $txt 既支持英文又支持中文呢。
function NbLines($w, $txt)
{
// Compute the number of lines a MultiCell of width w will take
if (!isset($this->CurrentFont))
$this->Error('No font has been set');
$cw = $this->CurrentFont['cw'];
if ($w == 0)
$w = $this->w - $this->rMargin - $this->x;
$wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
$s = str_replace("\r", '', (string)$txt);
$nb = strlen($s);
if ($nb > 0 && $s[$nb - 1] == "\n")
$nb--;
$sep = -1;
$i = 0;
$j = 0;
$l = 0;
$nl = 1;
while ($i < $nb) {
$c = $s[$i];
if ($c == "\n") {
$i++;
$sep = -1;
$j = $i;
$l = 0;
$nl++;
continue;
}
if ($c == ' ')
$sep = $i;
$l += $cw[$c];
if ($l > $wmax) {
if ($sep == -1) {
if ($i == $j)
$i++;
} else
$i = $sep + 1;
$sep = -1;
$j = $i;
$l = 0;
$nl++;
} else
$i++;
}
return $nl;
}