0

I am trying to insert a footnote in my generated document with phpword. Assume this:

$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));
}

And now I would like to have the text inside the brackets "[]" inserted as a footnote. I have NO idea how to get this handled. Maybe you understand this here: http://phpword.readthedocs.org/en/latest/elements.html#footnotes-endnotes

1 Answer 1

0

Ok, did it myself. But it doesn't mean it is foolproof:

$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));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.