我正在尝试使用phpword在我生成的文档中插入脚注。假设如下:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$content= "Line\r\nAnother Line\r\nYet another [and my footnote] line\r\nfinal line";
$section = $phpWord->addSection();
$textlines = preg_split("/(\r\n|\r|\n)/", $content);
foreach($textlines as $line) {
$section->addText(htmlspecialchars($line));
}现在,我希望将括号"[]“内的文本作为脚注插入。我不知道怎么处理这件事。也许你在这里明白这一点:http://phpword.readthedocs.org/en/latest/elements.html#footnotes-endnotes
发布于 2016-03-20 19:54:16
好吧,我自己做的。但这并不意味着它是万无一失的:
$content = "Line\r\nAnother Line\r\nYetß [önd my footnoteü] anotherß [änd my footnote2] line\r\nfinal line";
$section = $phpWord->addSection();
$textlines = preg_split("/(\r\n|\r|\n)/", $content);
foreach($textlines as $line) {
# check for footnotes
if (preg_match("/\[.+?\]/", $line)) {
$textrun = $section->addTextRun();
$chunk_array=preg_split("/([\[\]])/", $line, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($chunk_array as $chunk) {
# open
if ($chunk=="[") {
$openit=true;
$closeit=false;
continue;
}
if ($chunk=="]") {
$openit=false;
$closeit=true;
continue;
}
if ($openit) {
$footnote = $textrun->addFootnote();
$footnote->addText(htmlspecialchars($chunk));
} else {
$textrun->addText(htmlspecialchars(rtrim($chunk)));
}
#print $chunk."\n";
}
} else {
$section->addText(htmlspecialchars($line));
}
}https://stackoverflow.com/questions/36112438
复制相似问题