开发者

PHP If statement [duplicate]

This question already exists: Closed 11 years ago.

Possible Duplicate:

How to use X pages of PDF using FPDF?

I have a long text that is stored in $text.

My $text contains about 10 lines of string seperated with "\n".

I have to seperate this text into x of pages in PDF by using this code.

$textArray = explode("\n",$text);

$pdf1 = implode("\n", array_slice($textArray, 0, 2));

$pdf2 = implode("\n", array_slice($textArray, 2, 2));

$pdf3 = implode("\n", array_slice($textArray, 4, 2));

$pdf4 = implo开发者_JAVA百科de("\n", array_slice($textArray, 6, 2));

$pdf5 = implode("\n", array_slice($textArray, 8));

If all $pdf are filled in with strings (up to $pdf5), I want to store this value for:

$pdf_original = "forms\pdfForms\5pg.pdf";

BUT if the text/string only stored up $pdf4, and $pdf5 should be NULL or blank, I want to store this value for:

$pdf_original = "forms\pdfForms\4pg.pdf";

The list goes on until ONLY if I use $pdf1, then I want $pdf_original to be "forms\pdfForms\1pg.pdf";

Basically:

1pg.pdf = 1 page PDF = text will be stored up to $pdf1. $pdf2-5 will be NULL/Blank

2pg.pdf = 2 page PDF = text will be stored up to $pdf2. $pdf3-5 will be NULL/Blank

3pg.pdf = 3 page PDF = text will be stored up to $pdf3. $pdf4-5 will be NULL/Blank

4pg.pdf = 4 page PDF = text will be stored up to $pdf4. $pdf5 will be NULL/Blank

5pg.pdf = 5 page PDF = text will be stored up to $pdf5.


I think you are better off using a multidimensional array. This would distribute the lines evenly over the number of provided pages:

$pages = 5;
$textArray = explode("\n", $text);
$linesPerPage = ceil(count($textArray)/$pages);

$pages = array_chunk($textArray, $linesPerPage);
foreach($pages as $k => $page) {
    $pages[$k] = implode("\n", $page);
}

$pdf_original = sprintf("forms\pdfForms\%dpg.pdf", count($pages));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜