您好,欢迎访问一九零五行业门户网

PHPWord添加文本到指定位置

想导出word文档,将文本添加到文档的指定位置,比如前一段话的后面,而前一段话是循环生成的,该段后面又有循环生成的另一段话,而我想加入的文本不能放在循环里,只能在两次循环输出文本之后添加,这样的话我怎么才能把文本输出到紧接着的第一个循环之后?
例如:
我想将答案放在问题的后面,而问题和选项都是循环生成的,怎么才能实现将其放在“处分”的后面??
求大神赐教
部分代码:
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文档,将文本添加到文档的指定位置,比如前一段话的后面,而前一段话是循环生成的,该段后面又有循环生成的另一段话,而我想加入的文本不能放在循环里,只能在两次循环输出文本之后添加,这样的话我怎么才能把文本输出到紧接着的第一个循环之后?
例如:
我想将答案放在问题的后面,而问题和选项都是循环生成的,怎么才能实现将其放在“处分”的后面??
求大神赐教
部分代码:
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对象表示出来。
其它类似信息

推荐信息