2016-11-29 39 views
-4

我想讀取使用下面的代碼的docx文件,這是工作正常,但我的docx文件有粗體,標題等,但下面的代碼不顯示爲相同的單詞文件。不能像讀取docx文件一樣使用php

demo docx file

function read_file_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); 
    }// end while 

    zip_close($zip); 

    //echo $content; 
    //echo "<hr>"; 
    //file_put_contents('1.xml', $content); 

    $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; 
} 
$filename = "testing.docx";// or /var/www/html/file.docx 

$content = read_file_docx($filename); 
if($content !== false) { 

    $viewResume = nl2br($content); 

    $str = $viewResume; 
    $keyword = 'When'; 
    $str = preg_replace("/\b([a-z]*${keyword}[a-z]*)\b/i","<b style='color:#F00;'>$1</b>",$str); 
    echo "$str"; 
    echo preg_match_all('([^\.\!\?]+)',$str,$keyword); 


} 
else { 
    echo 'Couldn\'t the file. Please check that file.'; 
} 
+1

是什麼讓你覺得這段代碼_would_顯示任何格式? – duskwuff

+0

它顯示出正在運行的內容,如下所示....新更改 請檢查爲什麼我們可以用員工ID和密碼登錄爲客戶端。完成 在下面的圖片中,您可以看到,我只是以員工登錄詳細信息登錄爲客戶端。 搜索選項不起作用,以及它沒有顯示我們想要搜索的細節。完成 搜索可以通過名稱,聯繫電話,電子郵件ID,客戶端ID和所有狀態來完成。需要位時間 沒有評論員工不應該能夠更新狀態。完成 當我從員工更新客戶端時,我不被帶到同一字段 –

+1

好的,下一個問題。你寫了這個代碼嗎?如果不是,你是否至少對其工作原理有所瞭解? – duskwuff

回答

1

的docx是一個壓縮文件格式,這樣你就可以自己用一個簡單的unzipper解壓。

在那裏,你的主要文本是字/ document.xml中

元素有

  • <w:p>:第
  • <w:r>:運行(包含樣式)
  • <w:t>:這包含文字

在y我們目前的解決方案,你只是檢索文本(這是在<w:t>內。爲了獲得風格,你需要解析<w:t>。例如<w:u w:val=\"single\"/>表示加下劃線。

但是,這可能會變得相當複雜,所以我建議使用已經制定的解決方案,如Pandoc

相關問題