2012-11-25 23 views
0

我只是想知道是否有人能幫助我。我一直試圖弄清楚這一點。 我有這些變量php函數將描述分成多行68個字符長

$booking['occasion'] 
$booking['date'] 
$booking['venue'] 

,我希望做一個描述出他們的這個樣子。 「

$ description =」在$場館中爲 $日期提供$場合DJ的存款「;

我想顯示在PDF文件中像這樣

$pdf->writeHTMLCell(139, 5, '20', '125', $line1, '', 1, 1, true, 'L', true); 
$pdf->writeHTMLCell(139, 5, '25', '125', $line2, '', 1, 1, true, 'L', true); 
$pdf->writeHTMLCell(139, 5, '30', '125', $line3, '', 1, 1, true, 'L', true); 
$pdf->writeHTMLCell(139, 5, '35', '125', $line4, '', 1, 1, true, 'L', true); 
$pdf->writeHTMLCell(139, 5, '40', '125', $line5, '', 1, 1, true, 'L', true); 

我想每68個字符後以包裹描述到下一行超過3條或4線的描述,而是僅希望包裹完成之後詞語

任何人都可以幫助我創建一個功能,將desicprtion分成3或4行到目前爲止我知道這個代碼是我想要的。

$description = "Deposit for suppling a DJ and Equipment for a $occasion on the 
$date in $venue"; 
$decriptionLength = strlen($description); 
if($decriptionLength <= 68){ 
    $line1 = $description;  
} 
elseif($decriptionLength > 68) 
{ 
    $line1 = substr($description, 0, 68); 
    $line2 = substr($description, 68, 136); 
} 

這段代碼將包裝文本,即使在一個詞的中間,所以我不想這個。 我知道這可能是很多要問,但如果任何人都可以想出一些代碼,我將不勝感激。

+0

您是否嘗試過['換行()'](http://www.php.net/wordwrap)? –

回答

0

wordwrap()功能可能會有所幫助。

http://php.net/manual/en/function.wordwrap.php

$wordwrapped = wordwrap($description, 139, "\n"); 

$lines = explode("\n", $wordwrapped); 

$line1 = $lines[0]; 
$line2 = $lines[1]; 

if (count($lines) > 3) { 

    $third_line = ""; 

    for ($idx = 1; $idx < count($lines); $idx++) { 
     $third_line .= $lines[$idx] . " "; 
    } 

    $line3 = $third_line; 

} 

else { 

    $line3 = $lines[2]; 

} 
+0

感謝您的幫助。我在第二行發生錯誤「警告:implode()[function.implode]:無效參數傳遞」任何想法? –

+0

看到更新,使用了錯誤的功能。 –