Home >Backend Development >PHP Tutorial >PHPWord adds text to the specified location
I want to export a word document and add text to a specified position in the document, such as after the previous paragraph, which was generated in a loop. After this paragraph, there is another paragraph generated in a loop, and I want to add Text cannot be placed in a loop, it can only be added after the two loops output text. In this case, how can I output the text immediately after the first loop?
For example:
I want to put the answer after the question, and the questions and options are generated in a loop. How can I put it after the "punishment"? ?
Ask God for enlightenment
Part of the code:
<code>foreach ($ques['answer'] as $key => $an) { $textrun1 = $section->createTextRun('rStyle'); if (is_array($an['synopsis'])) { $textrun1->addText($this->aZ[$key] . ".", 'rStyle'); foreach ($an['synopsis'] as $i) { if (isset($i['type']) && $i['type'] == "text") { $textrun1->addText($i['value'], 'rStyle'); } if (isset($i['type']) && $i['type'] == "img" && file_exists($img = ROOT_DIR . str_replace(ROOT_URL, '', $i['value']))) { $imageStyle = array( 'width' => $i['width'] == 0 ? 350 : $i['width'], 'height' => $i['height'] == 0 ? 350 : $i['height'] // 'align' => 'center', ); $textrun1->addImage($img, $imageStyle); } } unset($textrun1); } else { $section->addText(strtoupper($this->aZ[$key]) . "." . $an['synopsis'], 'rStyle'); } if ($an['value']) { $answer[] = $this->aZ[$key]; } $section->addTextBreak(); } //判断是否输出答案 if($withAnswer==1){ $section->addText("(" . implode($answer, ",") . ")", 'rStyle'); $section->addTextBreak(); $answer = array(); }</code>
I want to export a word document and add text to a specified position in the document, such as after the previous paragraph, which was generated in a loop. After this paragraph, there is another paragraph generated in a loop, and I want to add Text cannot be placed in a loop, it can only be added after the two loops output text. In this case, how can I output the text immediately after the first loop?
For example:
I want to put the answer after the question, and the questions and options are generated in a loop. How can I put it after the "punishment"? ?
Ask God for enlightenment
Part of the code:
<code>foreach ($ques['answer'] as $key => $an) { $textrun1 = $section->createTextRun('rStyle'); if (is_array($an['synopsis'])) { $textrun1->addText($this->aZ[$key] . ".", 'rStyle'); foreach ($an['synopsis'] as $i) { if (isset($i['type']) && $i['type'] == "text") { $textrun1->addText($i['value'], 'rStyle'); } if (isset($i['type']) && $i['type'] == "img" && file_exists($img = ROOT_DIR . str_replace(ROOT_URL, '', $i['value']))) { $imageStyle = array( 'width' => $i['width'] == 0 ? 350 : $i['width'], 'height' => $i['height'] == 0 ? 350 : $i['height'] // 'align' => 'center', ); $textrun1->addImage($img, $imageStyle); } } unset($textrun1); } else { $section->addText(strtoupper($this->aZ[$key]) . "." . $an['synopsis'], 'rStyle'); } if ($an['value']) { $answer[] = $this->aZ[$key]; } $section->addTextBreak(); } //判断是否输出答案 if($withAnswer==1){ $section->addText("(" . implode($answer, ",") . ")", 'rStyle'); $section->addTextBreak(); $answer = array(); }</code>
Solved, use TextRun to represent the texts you want to connect together with the same textrun object.