首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用phpword插入脚注

使用phpword插入脚注
EN

Stack Overflow用户
提问于 2016-03-20 18:09:21
回答 1查看 382关注 0票数 0

我正在尝试使用phpword在我生成的文档中插入脚注。假设如下:

代码语言:javascript
复制
$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

EN

回答 1

Stack Overflow用户

发布于 2016-03-20 19:54:16

好吧,我自己做的。但这并不意味着它是万无一失的:

代码语言:javascript
复制
$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));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36112438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档