php 在php中读取word文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10646445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
read word document in php
提问by Othman
I'm doing a project now, and I'm stuck with reading word documents.
我现在正在做一个项目,我一直在阅读word文档。
Word File content.
Word 文件内容。
This is a test word file in PHP.
Thank you.
PHP code.
PHP代码。
$myFile = "wordfile.docx";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 1000);
fclose($fh);
echo $theData;
output:
输出:
PK!éQ°?[Content_Types].xml ¢( ′”MO?@??&t?fˉ|]e`??pP<*‰??v
?Yì,_?TiI?(ziòN?÷}fúT`??h?5)?&‘6Sf'2×?c|?"?d¢°R?d?t?Eo
?r? |l????′à?:0Té-×"D-?p'?§???tn?′& q(=X?÷1?!.é?
???,o_?WF¥L8W()ò2êu <"??l.T%¤?ìqa^N?p0ùKPol-*?3ó
?¢‘eáIhb??3?Y9ó?wr?1F??JB-/Y?·é;é"?+Z(3e?èaUt=?ú÷?
?7|?<I?H?<4?eóé:bG??!DN ùt??mC?s+?T_tb??$§ó4??
0?£?n…′#W×?ù?í±H:#oò???h{?JuLG? ê?Dt?êDZXg÷?Fì kè?????PK
!??'??_rel
IS there anyway to read the word document in PHP ?
有没有办法在 PHP 中阅读 word 文档?
回答by Sudhir
For docx use this function
对于 docx 使用此功能
function read_docx($filename){
$striped_content = '';
$content = '';
if(!$filename || !file_exists($filename)) return false;
$zip = zip_open($filename);
if (!$zip || is_numeric($zip)) return false;
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
if (zip_entry_name($zip_entry) != "word/document.xml") continue;
$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}
zip_close($zip);
$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
$content = str_replace('</w:r></w:p>', "\r\n", $content);
$striped_content = strip_tags($content);
return $striped_content;
}
It will return text from docx
它将从 docx 返回文本
回答by user2912903
"PHPWord is a library written in pure PHP that provides a set of classes to write to and read from different document file formats." (PHPOffice, 2016)
“PHPWord 是一个用纯 PHP 编写的库,它提供了一组用于写入和读取不同文档文件格式的类。” (PHPOffice,2016)
This open php library should solve your problem. you can eighter download it oder get it by composer:
这个开放的 php 库应该可以解决您的问题。你可以下载它或通过作曲家获取它:
回答by Francis Avila
"docx" is different from "doc". Docx files are basically xml files in a zipfile container (as described by wikipedia). Doc files are binary blobs.
“docx”与“doc”不同。Docx 文件基本上是 zipfile 容器中的 xml 文件(如维基百科所述)。Doc 文件是二进制 blob。
I am aware of no library that can easily read docx files in php (although Phpdocx can write them). However, since these are just zip files and xml files, you should be able do put something together using ZipArchiveto open the docx container and DOMDocumentor SimpleXMLor XMLReaderor XSLTProcessorto read the xml documents themselves.
我知道没有任何库可以轻松读取 php 中的 docx 文件(尽管Phpdocx 可以编写它们)。但是,由于这些只是 zip 文件和 xml 文件,因此您应该可以将一些东西放在一起,ZipArchive用于打开 docx 容器和DOMDocument/SimpleXML或XMLReader或XSLTProcessor读取 xml 文档本身。
回答by Andreas Wong
Word document isn't stored conveniently like a text file (it's more like xml / binary file), so you can't just use echo and expects it to output the human readable portion of the docxfile.
Word 文档不像文本文件那样方便存储(它更像是 xml/二进制文件),因此您不能只使用 echo 并期望它输出docx文件的人类可读部分。
There's a library that could do what you want, but it takes only docfile
有一个库可以做你想做的事,但它只需要doc文件

