どのようにしてdocxコンテンツを読み込み、すべてのタグを削除しますか?PHPはdocxファイルの内容を読み込みますが、改行、斜体、下線、太字のままにしていますか?
- 太字
- 斜体
- 下線
- 新線の下に
は、私は他の回答から得た私のコードです:これまでのところ、私は唯一の管理
//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>
を交換してい気づくでしょう。
もっと良い解決法はありますか?あなたの現在のソリューションを使用すると、整形の終わりを得ることはありませんので、タグを除去
上記の私の編集を参照してください。
は、今、私たちは右の出力を持っています。ありがとう。 – laukok