我对 php 比较陌生,需要一些帮助。
当用户可以提交文本时,我有一个 html 表单。此文本由 php 处理,如果有超过 20 个字符的行,则发生换行符。之后,所有文本都“爆炸”了,我得到了一个数组。到目前为止一切都很好。
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'];
$title= strtoupper($_POST['titre']);
$txtcontent = $_POST['texte'];
$txtcontent = wordwrap($txtcontent,20,hexToStr('0D0A'),true);
$txtcontent = explode("\n", $txtcontent);
print_r(array_chunk($txtcontent, 9, false));
$filecontent = implode(array_chunk($txtcontent, 9, false));
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 将数组块中的数组 $txtcontent 与 9 个元素分开。
现在,我需要根据键值在每个新数组元素的开头和结尾添加一些常量文本。也许我需要做一个循环或类似的事情,我已经尝试了很多次但仍然不能。我需要帮助才能做到这一点。
示例:如果我在网络表单中提交此文本:
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12
我会有:
Array ( [0] => Array ( [0] => line1 [1] => line2 [2] => line3 [3] => line4 [4] => line5 [5] => line6 [6] => line7 [7] => line8 [8] => line9 ) [1] => Array ( [0] => line10 [1] => line11 [2] => line12 ) )
这是我需要帮助的地方:
处理后(添加常量文本后,根据键值),应该是这样的。但是我不知道我该怎么做。
Array ( [0] => Array (
[0] => \Text 1,1,"line1"
[1] => \Text 7,1,"line2"
[2] => \Text 13,1,"line3"
[3] => \Text 19,1,"line4"
[4] => \Text 25,1,"line5"
[5] => \Text 31,1,"line6"
[6] => \Text 37,1,"line7"
[7] => \Text 43,1,"line8"
[8] => \Text 49,1,"line9"
) [1] => Array (
[0] => \Text 1,1,"line10"
[1] => \Text 7,1,"line11"
[2] => \Text 13,1,"line12"
) )
最后,当我将它输出到文件时,我将拥有:
(我不知道如何输出array_chunk,我也需要帮助才能做到这一点。)
\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"
请注意,数组中的元素编号可能更大或更小,这只是一个示例。
另请注意,具有相同键的数组(例如:第 1 行和第 10 行)将在末尾和开头具有相同的额外文本。
我认为这个问题很难,但我希望有人能帮助我。我已经在一周前试图解决它,但我仍然无法解决。