I am Trying to read a docx and **split it into parts first using the docxConversion class ** The first function is read_docx
private function read_docx(){
$striped_content = '';
$content = '';
$zip = zip_open($this->filename);
if (!$zip || is_numeric($zip)) return false;
$zip_entry = zip_read($zip);
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);
$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
$content = str_replace('</w:r></w:p>', "\r\n", $content);
$content = str_replace("</w:p>", "\r\n", $content);
$pattern = "/(الفقرة\s[0-9]+\s)|(الأولى الفقرة)./u";//المادة\s*\d:
$striped_content = strip_tags($content);
$splitted_para_arr = preg_split($pattern,$striped_content,null,PREG_SPLIT_NO_EMPTY);
return $splitted_para_arr;//striped_content;
}
The Second Function is convert to text
public function convertToText() {
if(isset($this->filename) && !file_exists($this->filename)) {
return "File Not exists";
}
$fileArray = pathinfo($this->filename);
$file_ext = $fileArray['extension'];
if($file_ext == "docx") {
return $this->read_docx();
} else {
return "Invalid File Type";
}
}
Then split each part into paragraphs using the following function
public function getParag($article){
$splitted_para_arr = preg_split("/\.\n/u",$article,null,PREG_SPLIT_NO_EMPTY);
return $splitted_para_arr;//striped_content;
}
But the problem here is I can't get the paragraphs with the following pattern "/.\n/u"
If you want to deal with any kind of linebreak, use \R
:
$splitted_para_arr = preg_split("/\.\R/", $article, null, PREG_SPLIT_NO_EMPTY);
\R
matches \n
, \r
or \r\n