Split a large string (article page) into array (pages) with PHP
I have a very large string that has a couple "\n" line break per paragraph.
I have 3-4 pages (3-4 fields) that I want to insert the article into, but if it does not fit in the 1st page (1st field), I want to continue the string data into the 2nd page (2nd field).
1st page (1st field) has 14 lines 2nd page - 4th page (1st field - 4th field) has 48 lines each page/field.
I have tried str_split, but it has not work for me :(
Here's a sample:
$string = "FIRST PARAGRAPH" . "\N\N" "2ND PARAGRAPH" . "\N\N" . "AND SOOOO ON... ";
// i want to split this string into 4 sections of fields.
$field1 = (has a max of 14 lines, don't know how many characters tho);
$field2 = (has a max of 48 lines, don't know how many characters too!);
$field3 = (has a max of 48 lines, don't know how many characters too!);
$field4 = (has a max of 48 lines, don't know how many characters too!);
this will be filled in to ADOBE PDF'开发者_开发百科s 4 sections/fields using FPDF.
You need to find something you can split the string on. If each line is deliminated by \n\n then you could do
$lines = explode("\n\n", $string);
Then line one would be $lines[0], line two would be $lines[1] and so on. You can then use array_slice() to split the strings up in to the field variables.
I'm not 100% sure to understand your requirements but a script as the following one should work, except that the result is into an array and not into separate variables.
// uncomment the following line to get rid of paragraph breaks;
// $string = strtr($string, array("\n\n","\n"));
$lines = explode("\n", $string);
$pagelen = array(14,48,48,48);
$result = array();
foreach($pagelen as $cPageLen)
array_push($result, join("\n",array_splice($lines, 0, $cPageLen)));
精彩评论