2015-11-25 48 views
0

開始之前..我對PHP很新穎......希望你能忍受我這件事。在閱讀php中的docx文件時刪除空行

我有一個文件(的類型.docx)的句子,我分裂的地方有一段時間。

我使用的代碼是:

$docObj = new Filetotext($fileToTest); 
$docextracted = $docObj->extractText(); 

// pattern to find the fullstop 
$pattern = '/\./'; 
//giving a new line to each sentence 
$current1= preg_replace($pattern, "\r\n", $docextracted); 

$splitArray = explode("\n", $current1); 
//$mainFile = $splitArray; 
$mainFile = (str_replace(' ', '', $splitArray)); 
print_r($mainFile); 

該文件實際上包含以下內容:(僅用於樣品的目的)

This is a test file. The purpose of this test file is to ensure that the file reading part is working. This test is important. This test ends here. 

然而,當print_r($mainFile);給出了以下內容:

Array 
(
    [0] => 
    [1] => Thisisatestfile 
    [2] => Thepurposeofthistestfileistoensurethatthefilereadingpartisworking 
    [3] => Thistestisimportant 
    [4] => Thistestendshere 
    [5] => 
) 

第一個和最後一個數組索引中的空白部分(忘記它)是問題。我嘗試了其他文件和相同的東西。第一個和最後一個索引是空的。這導致了一個問題,當我試圖把一個計數器,或者當我試圖比較數組與其他數組。

我的代碼是否帶有空白部分?

任何形式的幫助深表感謝:)

+0

str_replace建議而不是空白,第一個(第0)和第5個索引中有空格?你可以嘗試用\ r \ n爆炸而不是\ n嗎? – Daniel

回答

1

做在$當前1修剪之前和之後刪除空格,爆炸前(),應該做的伎倆。

.... 
$current1 = trim($current1); 
$splitArray = explode("\n", $current1); 
.... 
+0

這工作! 另外嘗試array_filter($ mainFile)建議由另一個用戶(誰已經刪除了答案。)它的工作一半。刪除最後一個索引。但這適用於兩個! :) 謝謝 – Tsar