在PHP中读取Word文档 [英] read word document in php

查看:82
本文介绍了在PHP中读取Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在做一个项目,而且我一直在阅读Word文档.

I'm doing a project now, and I'm stuck with reading word documents.

Word文件的内容.

Word File content.

This is a test word file in PHP.

Thank you.

PHP代码.

    $myFile = "wordfile.docx";
    $fh = fopen($myFile, 'r');
    $theData = fread($fh, 1000);
    fclose($fh);
    echo $theData;

输出:

PK!éQ°Â[Content_Types].xml ¢( ´"MOÂ@†ï&þ‡f¯¦]ð`Œ¡pP<*‰Ïëv
 «Ýì,_ÿÞiI¡(ziÒNß÷}fÚÞ`©‹h•5)ë&‘6Sf’²×ñc|Ë"Âd¢°R¶dƒþåEo
 ¼r€© ¦l‚»ãå´ÀÄ:0TÉ­×"ЭŸp'䧘¿îtn¸´&€  q(=X¿÷¹˜!.éñ
 š„ä,º_¿WF¥L8W()ò²Êu <"œ›l.Þ%¤¬Ìqª^Nøp0ÙKPºl­*Õ3Ó
 «¢‘ðáIhbçë3žY9ÓÔwr¼¹F›çJB­/Ýœ·é;é"©+Z(³e?ÈaUþ=ÅÚ÷Ä
 ø7¦Ã<I?Hû<4ÆeÓÉ:bGÛž!ÐN    ùþÛÆmCÇs+ÂÞ_þbǼ$§ó4ïœ
 0ñ£¶n…´#€W×îٕͱH:#oÒÎñ¿h{»JuLGÎ êõÐtÄêDZXg÷åFÌ kÈæÕîÿÿPK
 !ÇÂ'¼ß_rel

反正有没有用PHP阅读word文档?

IS there anyway to read the word document in PHP ?

推荐答案

对于docx,请使用此功能

For docx use this function

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

它将从docx返回文本

It will return text from docx

这篇关于在PHP中读取Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆