I'm trying to display some data on a PDF document with FPDF, my problem is I can't limit the number of characters of a string and sometimes the width is exceeds, I already use MultiCell but I whant to set a limit of characters,
I tried to solve this with my function custom echo but apparently doesn't work with fpdf I don't know what happen.
function custom_echo($x, $length)
{
if(strlen($x)<=$length)
{
echo $x;
}
else
{
$y=substr($x,0,$length) . '...';
echo $y;
}
}
$message= "HELLO WORLD";
$pdf=new FPDF();
$pdf->SetLeftMargin(0);
$pdf->AddPage();
$pdf->MultiCell( 95, 6, utf8_decode(custom_echo($message,5)), 0, 1);
// already tried this
$pdf->MultiCell( 95, 6, custom_echo(utf8_decode($message),5), 0, 1);
$pdf->Output();
The PHP echo
command sends a string to output. You need to return the string as the result of the function so that FPDF can use it.
function custom_echo($x, $length) {
if (strlen($x) <= $length) {
return $x;
} else {
return substr($x,0,$length) . '...';
}
}
which can be simplified to:
function custom_echo($x, $length) {
if (strlen($x) <= $length) {
return $x;
}
return substr($x,0,$length) . '...';
}
and could be made even shorter but that is how I would do it.