techdealer Posted December 31, 2012 Share Posted December 31, 2012 (edited) I'm having trouble adding text at the end of the latest array chunk, when he has less than 9 elements + counter. Below is my actual code. I use the $txtcontent as text input from a webform. function hexToStr($hex) { $string=''; for ($i=0; $i < strlen($hex)-1; $i+=2) { $string .= chr(hexdec($hex[$i].$hex[$i+1])); } return $string; }[/font][/color] $commas = hexToStr('22'); $format = $_POST['format']; $titre= strtoupper($_POST['titre']); $txtcontent = $_POST['texte']; $txtcontent = wordwrap($txtcontent,20,hexToStr('0D0A'),true); $txtcontent = explode("\n", $txtcontent); $input = array_chunk($txtcontent, 9, false); $i = 1; foreach($input as $key => $array){ $j = 1; $input[$key][0] = "\If S=".$i."\n"; foreach($array as $k => $v){ $input[$key][$k+1] = '\text '.($k*6+1).',1,'.$commas.str_replace(hexToStr('0D'), "", $v).$commas.hexToStr('0D0A'); $j++; } if ($j == 10) $input[$key][10] = "\IfEnd\n"; $i++; } function array_flatten($array) { if (!is_array($array)) { return FALSE; } $result = array(); foreach ($array as $key => $value) { if (is_array($value)) { $result = array_merge($result, array_flatten($value)); } else { $result[$key] = $value; } } return $result; } $array = array_flatten($input); $filecontent = implode($array); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment;filename='.$_POST['titre'].'.'.$format.''); header('Cache-Control: max-age=0'); //$fh = fopen($filename, 'wb'); $fh = fopen('php://output', 'wb'); fwrite($fh, $filecontent); fclose($fh); Exemple: if i sumbit this content as input in the webform: line1 line2 line3 line4 line5 line6 line7 line8 line9 line10 line11 line12 line13 line14 line15 line16 line17 line18 line19 line20 I will have a file with this content: \If S=1 \text 1,1,"line1" \text 7,1,"line2" \text 13,1,"line3" \text 19,1,"line4" \text 25,1,"line5" \text 31,1,"line6" \text 37,1,"line7" \text 43,1,"line8" \text 49,1,"line9" \IfEnd \If S=2 \text 1,1,"line10" \text 7,1,"line11" \text 13,1,"line12" \text 19,1,"line13" \text 25,1,"line14" \text 31,1,"line15" \text 37,1,"line16" \text 43,1,"line17" \text 49,1,"line18" \IfEnd \If S=3 \text 1,1,"line19" \text 7,1,"line20" But i also need that the latest array chunk contain the text "\ifEnd" at the end, even when he has less than 9 elements + counter (S= X). \If S=1 \text 1,1,"line1" \text 7,1,"line2" \text 13,1,"line3" \text 19,1,"line4" \text 25,1,"line5" \text 31,1,"line6" \text 37,1,"line7" \text 43,1,"line8" \text 49,1,"line9" \IfEnd \If S=2 \text 1,1,"line10" \text 7,1,"line11" \text 13,1,"line12" \text 19,1,"line13" \text 25,1,"line14" \text 31,1,"line15" \text 37,1,"line16" \text 43,1,"line17" \text 49,1,"line18" \IfEnd \If S=3 \text 1,1,"line19" \text 7,1,"line20" \IfEnd Anyone have any suggestions on how to do it? Edited December 31, 2012 by techdealer Quote Link to comment https://forums.phpfreaks.com/topic/272562-php-trouble-to-add-text-at-the-end-of-the-array-with-less-than-9-elements/ Share on other sites More sharing options...
requinix Posted December 31, 2012 Share Posted December 31, 2012 Easiest way? After the loops, if there weren't a multiple of 9 total items then add the \EndIf. Most alternatives I can think of involve restructuring the code (a bit). Quote Link to comment https://forums.phpfreaks.com/topic/272562-php-trouble-to-add-text-at-the-end-of-the-array-with-less-than-9-elements/#findComment-1402478 Share on other sites More sharing options...
techdealer Posted January 1, 2013 Author Share Posted January 1, 2013 Easiest way? After the loops, if there weren't a multiple of 9 total items then add the \EndIf. Thanks, your suggestion worked. It is solved Quote Link to comment https://forums.phpfreaks.com/topic/272562-php-trouble-to-add-text-at-the-end-of-the-array-with-less-than-9-elements/#findComment-1402491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.