2016-07-15 18 views
0

どのようにしてdocxコンテンツを読み込み、すべてのタグを削除しますか?PHPはdocxファイルの内容を読み込みますが、改行、斜体、下線、太字のままにしていますか?

  1. 太字
  2. 斜体
  3. 下線
  4. 新線の下に

は、私は他の回答から得た私のコ​​ードです:これまでのところ、私は唯一の管理

//FUNCTION :: read a docx file and return the string 
// http://stackoverflow.com/questions/4587216/how-can-i-convert-a-docx-document-to-html-using-php 
// https://www.jackreichert.com/2012/11/how-to-convert-docx-to-html/ 
function readDocx($filePath) { 
    // Create new ZIP archive 
    $zip = new ZipArchive; 
    $dataFile = 'word/document.xml'; 
    // Open received archive file 
    if (true === $zip->open($filePath)) { 
     // If done, search for the data file in the archive 
     if (($index = $zip->locateName($dataFile)) !== false) { 
      // If found, read it to the string 
      $data = $zip->getFromIndex($index); 
      // Close archive file 
      $zip->close(); 
      // Load XML from a string 
      // Skip errors and warnings 
      $xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING); 
      // Return data without XML formatting tags 
      $xmldata = $xml->saveXML(); 
      // </w:p> is what word uses to mark the end of a paragraph. E.g. 
      // <w:p>This is a paragraph.</w:p> 
      // <w:p>And a second one.</w:p> 
      // http://stackoverflow.com/questions/5607594/find-linebreaks-in-a-docx-file-using-php 
      $xmldata = str_replace("</w:p>", "\r\n", $xmldata); 
      $xmldata = str_replace("<w:i/>", "<i>", $xmldata); 

      $contents = explode('\n',strip_tags($xmldata, "<i>")); 
      $text = ''; 
      foreach($contents as $i=>$content) { 
       $text .= $contents[$i]; 
      } 
      return $text; 
     } 
     $zip->close(); 
    } 
    // In case of failure return empty string 
    return ""; 
} 

$filePath = 'sample.docx'; 
$string = readDocx($filePath); 
var_dump($string); 

改行を保持するが残りの部分は保持しない:

$xmldata = str_replace("</w:p>", "\r\n", $xmldata); 
$xmldata = str_replace("<w:i/>", "<i>", $xmldata); // will get <i>Hello World <-- no closing i 

アイデアはありますか?

EDIT:

$xmldata = preg_replace("/<w\:i\/>(.*?)<\/w\:r>/is", "<i>$1</i>", $xmldata); 
$xmldata = preg_replace("/<w\:b\/>(.*?)<\/w\:r>/is", "<b>$1</b>", $xmldata); 
$xmldata = preg_replace("/<w\:u (.*?)\/>(.*?)<\/w\:r>/is", "<u>$2</u>", $xmldata); 

しかし、上記の解決策はあるため、たとえば欠陥を持っている:

<w:r><w:t xml:space="preserve"><w:i/>Hello</w:t></w:r><w:r><w:t xml:space="preserve"> World</w:t></w:r> 

<w:i/>がペアリングされていないので、あなたは、私が<w:i/><\/w\:r>を交換してい気づくでしょう。

もっと良い解決法はありますか?あなたの現在のソリューションを使用すると、整形の終わりを得ることはありませんので、タグを除去

答えて

0

が、良い方法ではありません - あなたの代わりにXMLを解釈について考える必要があります

あなたが探して他のタグは<w:b/>(太字)と<w:u ...>です(下線)

+0

上記の私の編集を参照してください。

< # Match an opening tag ([ibu]) # (1) Any type except 'p' > # Up to closing character (?= # Which is immediately followed by (?: \s* <[ibu]> \s*)*? # Another opening tag (or nothing) <\1> # And then its own closing tag. ) # End of lookahead | # Or match </ # A closing tag ([ibu]) # (2) Any type except 'p' > # Up to closing character (?= # Which is immediately followed by (?: \s* </ [ibu] > \s*)*? # Another closing tag (or nothing) </? \2 > # And then the same closing tag ) # End of lookahead | # Or match <p></p> # Empty <p> tags 

は、今、私たちは右の出力を持っています。ありがとう。 – laukok

0

私はこれらのソリューションを持っている - それは醜いですが、それは動作します:

 $xmldata = 
        '<w:r> 
     <w:rPr> 
     <w:u/> 
     <w:b/> 
     <w:i/> 
     </w:rPr> 
     <w:t>I feel that there is much to be said for the Celtic belief that the souls of those whom we have lost are held captive in some inferior being...</w:t> 
     </w:r>'; 
     // </w:p> is what word uses to mark the end of a paragraph. E.g. 
     // <w:p>This is a paragraph.</w:p> 
     // <w:p>And a second one.</w:p> 
     // http://stackoverflow.com/questions/5607594/find-linebreaks-in-a-docx-file-using-php 
     // http://officeopenxml.com/WPtext.php 
     $xmldata = str_replace("</w:p>", "\r\n", $xmldata); 
     $xmldata = preg_replace("/<w\:i\/>(.*?)<w:t(.*?)>(.*?)<\/w\:t>/is", "<w:i/>$1<w:t$2><i>$3</i></w:t>", $xmldata); 
     $xmldata = preg_replace("/<w\:b\/>(.*?)<w:t(.*?)>(.*?)<\/w\:t>/is", "<w:b/>$1<w:t$2><b>$3</b></w:t>", $xmldata); 
     $xmldata = preg_replace("/<w\:u(.*?)\/>(.*?)<w:t(.*?)>(.*?)<\/w\:t>/is", "<w:u$1/>$2<w:t$3><u>$4</u></w:t>", $xmldata); 

出力:

<u><b><i>I feel that there is much to be said for the Celtic belief that the souls of those whom we have lost are held captive in some inferior being...</i></b></u> 
1

私はので、私は1つのstrip_tags()を行い、それらのstr_repalce()explode()機能の必要性を見ていない:

$contents = strip_tags($xmldata, '<w:p><w:u><w:i><w:b>'); 

今ではあなたが必要なすべてのタグが保存されていることを確認しています。別のステップを取ると、我々はそれに対応するHTMLタグと<w:*>タグを置き換える必要があります。

$contents = preg_replace("/(<(\/?)w:(.)[^>]*>)\1*/", "<$2$3>", $contents); 

私たちは、自分の名前の1つの文字<p><b><i><u>でのみHTMLタグを持っていることはとても自分の名前をキャプチャして使用するのと同じくらい簡単ですドットキャプチャグループ:私はそれが高いのpを持っていて見つけたので

(    # (1 start) 
     <    # Match XML opening tag character   
     (\/?)  # (2) Match if it is going to be an ending tag 
     w:   # Literal `w:` 
     (.)   # (3) Match b,p,u,i 
     [^>]* >  # Up to closing tag character 
)    # (1 end) 
\1*    # Match if latter group repeats 

は、私は同じマッチしたタグ\1*をチェックしなければなりませんでした起こる可能性。私たちのdocxファイルは、以下のような3行が含まれている場合:イタリック

太字

ノーマル

次に、この時点で私たちの出力は次のようになります。

<p><b><b>Bold</p><p><i><i>Italic</p><p>Normal</p> 

ご覧のとおり、n個の複製されていないタグがペアになっていますすべてで良い。私たちは文書を整理する必要があります。しかしどうですか? PHP Tidyの拡張子によって

  1. PHP Tidyのが仕事のこの種のために素晴らしいですが、私は私たちの仕事を行うためのDOMDocumentがより適していますがDOMDocumentオブジェクト

に私たちのHTMLをロードします。

$dom = new DOMDocument; 
@$dom->loadHTML($contents, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); 
$contents = $dom->saveHTML(); 

DOCTYPEと、<html>/が不要なので、2つの関連するフラグが設定されていますタグ。この時点で

私たちの出力:

<p><b><b>Bold</b></b><p><i><i>Italic</i></i></p><p>Normal</p></p> 

良いニュースは、今、私たちは、タグをpairdきているが、それは我々が不要に開かれたタグを持つ悪いニュースかもしれません:作業のために

<p><b><b>Bold</b></b><p><i><i>Italic</i></i></p><p>Normal</p></p> 
^^    ^^ 

私は別の書き出しタグを取り除いて、別の書き込みをしました。RegEx:

$contents = preg_replace('~<([ibu])>(?=(?:\s*<[ibu]>\s*)*?<\1>)|</([ibu])>(?=(?:\s*</?[ibu]>\s*)*?</?\2>)|<p></p>~s', "", $contents); 

それは何をするつもりですここで見ること:一緒にすべてのものを置く

<p><b>Bold</b><p><i>Italic</i></p><p>Normal</p></p> 

<?php 

function readDocx($filePath) { 
    // Create new ZIP archive 
    $zip = new ZipArchive; 
    $dataFile = 'word/document.xml'; 
    // Open received archive file 
    if (true === $zip->open($filePath)) { 
     // If done, search for the data file in the archive 
     if (($index = $zip->locateName($dataFile)) !== false) { 
      $data = $zip->getFromIndex($index); 
      $zip->close(); 

      $dom = new DOMDocument; 
      $dom->loadXML($data, LIBXML_NOENT 
       | LIBXML_XINCLUDE 
       | LIBXML_NOERROR 
       | LIBXML_NOWARNING); 

      $xmldata = $dom->saveXML(); 

      $contents = strip_tags($xmldata, '<w:p><w:u><w:i><w:b>'); 
      $contents = preg_replace("/(<(\/?)w:(.)[^>]*>)\1*/", "<$2$3>", $contents); 

      $dom = new DOMDocument; 
      @$dom->loadHTML($contents, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); 
      $contents = $dom->saveHTML(); 

      $contents = preg_replace('~<([ibu])>(?=(?:\s*<[ibu]>\s*)*?<\1>)|</([ibu])>(?=(?:\s*</[ibu]>\s*)*?</?\2>)|<p></p>~s', "", $contents); 

      return $contents; 
     } 
     $zip->close(); 
    } 
    // In case of failure return empty string 
    return ""; 
} 

$filePath = 'sample.docx'; 
$string = readDocx($filePath); 
echo $string; 
関連する問題