跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

PHPWord添加文本到指定位置

想导出word文档,将文本添加到文档的指定位置,比如前一段话的后面,而前一段话是循环生成的,该段后面又有循环生成的另一段话,而我想加入的文本不能放在循环里,只能在两次循环输出文本之后添加,这样的话我怎么才能把文本输出到紧接着的第一个循环之后? 例如: 立即学习 “ PHP免费学习笔记(深入) ”; PHP 8.5.5 PHP 8.5.5 是 PHP 8.5 分支的维护更新版本。该版本延续了“小步快跑”的迭代逻辑,通过深度错误修复、底层性能微调以及安全加固,旨在为开发者提供一个更健壮、更高效的运行环境。该版本严格遵守语义化版本规范,不包含破坏性变更。 下载 我想将答案放在问题的后面,而问题和选项都是循环生成的,怎么才能实现将其放在“处分”的后面?? 求大神赐教 部分代码:
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(); }
回复内容: 想导出word文档,将文本添加到文档的指定位置,比如前一段话的后面,而前一段话是循环生成的,该段后面又有循环生成的另一段话,而我想加入的文本不能放在循环里,只能在两次循环输出文本之后添加,这样的话我怎么才能把文本输出到紧接着的第一个循环之后? 例如: 立即学习 “ PHP免费学习笔记(深入) ”; 我想将答案放在问题的后面,而问题和选项都是循环生成的,怎么才能实现将其放在“处分”的后面?? 求大神赐教 部分代码:
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(); }
已解决,采用TextRun,将想要连接在一起的文本用同一个textrun对象表示出来。

相关文章