我如何检索文本从一个文档文件php


How do I retrieve text from a doc file php

我试图从使用phpdoc文件检索文本。这是我正在使用的代码:

    function read_doc() {
        foreach (glob("*.doc") as $filename) {
            $file_handle = fopen($filename, "r"); //open the file
            $stream_text = @fread($file_handle, filesize($filename));
            $stream_line = explode(chr(0x0D),$stream_text);
            $output_text = "";
            foreach($stream_line as $single_line){
                $line_pos = strpos($single_line, chr(0x00));
                if(($line_pos !== FALSE) || (strlen($single_line)==0)){
                    $output_text .= "";
                }else{
                    $output_text .= $single_line." ";
                }
            }
            $output_text = preg_replace("/[^a-zA-Z0-9's','.'-'n'r't@'/'_'(')]/", "", $output_text);
            echo $output_text;
        }
}

我得到这样的结果:

超链接mailtoAnother@email.us Another@email.us Y, dXiJ(x(I_TS1EZBmU/xYy5g/GMGeD3Vqq8K)fw9 xrxwrTZaGy8IjbRcXI u3KGnD1NIBs RuKV。elm2fivlu8zh (W u4 (Tn 7_m-UBww_8(/0hFL)7iAs),Qg20ppf DU4p MDBJlC5 2FhsFYn3E6945Z5k8Fmw-dznZxJZp/P,)KQk5qpN8KGbe Sd17 paSR 6Q

是否有一些解决方案可以清除这一点,所以它从doc文件返回只是一个string的文本?

Doc文件很难处理香草php。

使用https://github.com/alchemy-fr/PHP-Unoconv我完成了你需要的。它实际上会检测不同的格式,并为您生成一个漂亮的xml。文档可以在这里找到

如果你搜索"unoconv" + "php"在网上也有很多这样的例子

用代码解析MS Word文档是很困难的。

这是因为MS在其格式中嵌入了大量数据,使其在您回显解析的单词/段落时看起来像胡言乱语。

我建议你尝试一个包库(来自packagist)来帮助你使用这个Word-Doc-Parser

可以很容易地通过composer安装,如果你的系统上有