2015-11-25 49 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