Php - 在每个array_chunk的开头添加一个计数器,在结尾添加一个文本

时间:2012-12-31 15:31:12

标签: php arrays forms

我在每个array_chunk的开头添加一个计数器,最后在文本中添加一个文本。

以下是我的实际代码。我使用$ txtcontent作为webform的文本输入。

        function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}



    $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);
foreach($input as $key => $array){
    foreach($array as $k => $v){
        $input[$key][$k] = '\text '.($k*6+1).',1,'.$commas.str_replace(hexToStr('0D'), "", $v).$commas.hexToStr('0D0A');
    }
}
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);

我想在每个array_chunk的前面添加一个计数器,在最后添加一个文本。

例: 如果我将此内容汇总为webform中的输入:

line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13
line14
line15
line16
line17
line18
line19
line20

我将有一个包含此内容的文件:

\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"
\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"
\text 1,1,"line19"
\text 7,1,"line20"

但是,我希望有类似下面的代码。在每个块的开头应该添加计数器,如\ S = 1,...和最后,文本\ IfEnd

\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"

请注意,输入始终可以大于20行。

有人对如何做有任何建议吗?

1 个答案:

答案 0 :(得分:1)

我尝试使用以下代码代替您的foreach循环,最后使用您描述的相同输出(减去引号):

$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++;
}

我希望这会有所帮助。基本上我在你的数组中添加了额外的键值对,以获得每次迭代的If和IfEnd,并保留一些计数。