PHP中的.ppt / .pptx解析器或XML转换器

时间:2016-02-21 23:59:14

标签: php xml converter powerpoint

我试图找到一种计算.ppt / .pptx文件单词的方法,但无法弄清楚该怎么做。我通过下面的功能,但似乎没有工作:

function parsePPT($filename) {
    $fileHandle = fopen($filename, "r");
    $line = @fread($fileHandle, filesize($filename));
    $lines = explode(chr(0x0f),$line);
    $outtext = '';

    foreach($lines as $thisline) {
        if (strpos($thisline, chr(0x00).chr(0x00).chr(0x00)) == 1) {
            $text_line = substr($thisline, 4);
            $end_pos   = strpos($text_line, chr(0x00));
            $text_line = substr($text_line, 0, $end_pos);
            $text_line = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$text_line);
            if (strlen($text_line) > 1) {
                $outtext.= substr($text_line, 0, $end_pos)."\n";
            }
        }
    }
    return $outtext;
}

如果不直接从ppt输出文本中计算,它应该是一种以XML或其他格式转换它然后轻松输出文本的方法吗?

提前感谢您的帮助!

更新:以下函数为.PPTX文件执行作业,但.PPT:

function parsePPT($input_file){
    $zip_handle = new ZipArchive;
    $output_text = "";
    if(true === $zip_handle->open($input_file)){
        $slide_number = 1; //loop through slide files
        while(($xml_index = $zip_handle->locateName("ppt/slides/slide".$slide_number.".xml")) !== false){
            $xml_datas = $zip_handle->getFromIndex($xml_index);
            $xml_handle = DOMDocument::loadXML($xml_datas, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            $output_text .= strip_tags($xml_handle->saveXML());
            $slide_number++;
        }
        if($slide_number == 1){
            $output_text .="";
        }
        $zip_handle->close();
    }else{
    $output_text .="";
    }
    return $output_text;
}

0 个答案:

没有答案