2

我无法在每个 array_chunk 的开头添加一个计数器并在末尾添加一个文本。

下面是我的实际代码。我使用 $txtcontent 作为来自网络表单的文本输入。

        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 的开头添加一个计数器,并在末尾添加一个文本。

示例:如果我将此内容作为输入汇总到网络表单中:

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"

但是,我想要下面的代码。在每个块的开头应添加计数器,如 \If 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 行。

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

4

1 回答 1

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,并保留一些计数。

于 2012-12-31T15:53:14.210 回答