2016-03-20 7 views
0

phpwordで脚注を作成した文書に挿入しようとしています。 この仮定: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

答えて

0

わかりました。しかし、それが完全であることを意味するものではありません。

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