在php中读取docx文件时删除空行


Removing empty line when reading docx file in php

启动前。。我对PHP很陌生。。。希望你能容忍我。

我有一个(.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] =>
)

第一个和最后一个数组索引中的空部分(忘记了单词)就是问题所在。我试过其他文件和同样的东西。第一个和最后一个索引为空。当我试图对此设置计数器时,或者当我试图将该数组与其他数组进行比较时,这会导致问题。

我的代码带来了空的部分,有什么问题吗?

任何形式的帮助都非常感谢:)

对$current1进行修剪以删除explode()之前和之后的空白应该可以做到。

....
$current1 = trim($current1);
$splitArray = explode("'n", $current1);
....