PHP, 将 .docx 拆分为段落 (PREG_SPLIT , "/\.\n/u")
PHP, split .docx into paragraphs (PREG_SPLIT , "/\.\n/u")
我正在尝试 阅读 docx 并 **首先使用 docxConversion class 将其拆分成多个部分 **
第一个函数是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";
}
}
然后使用以下函数将每个部分拆分成段落
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"
如果您想处理任何类型的换行符,请使用 \R
:
$splitted_para_arr = preg_split("/\.\R/", $article, null, PREG_SPLIT_NO_EMPTY);
\R
匹配 \n
、\r
或 \r\n
我正在尝试 阅读 docx 并 **首先使用 docxConversion class 将其拆分成多个部分 ** 第一个函数是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";
}
}
然后使用以下函数将每个部分拆分成段落
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"
如果您想处理任何类型的换行符,请使用 \R
:
$splitted_para_arr = preg_split("/\.\R/", $article, null, PREG_SPLIT_NO_EMPTY);
\R
匹配 \n
、\r
或 \r\n