【发布时间】:2015-09-07 08:06:11
【问题描述】:
我正在开发一个函数,该函数从 Microsoft Word .docx 文件中获取内容并将它们显示在 web page 中。文本呈现良好,但我似乎无法让它显示换行符?
我的函数代码如下:
function readDocx($filePath) {
$zip = new ZipArchive;
//Create new ZIP archive
$dataFile = "word/document.xml";
//Open received archive file
if (true === $zip->open($filePath)) {
//if open successful, search for the data file inside the archive
if (($index = $zip ->locateName($dataFile)) !== false) {
//if found, read it to the string
$data = $zip->getFromIndex($index);
//load XML from a string. skips errors and warnings
$xml = new DOMDocument();
$xml->loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
$xmldata = $xml->saveXML();
$xmldata = str_replace("</w:p>", "^^^^^^" . "
" . "<br>" . "\n" . "<br />" . "<p>" . "%%%", $xmldata);
echo strip_tags($xmldata);
}
$zip->close();
}
//in case of failure, return an empty string
else {
echo "An error has occurred while opening the file - please try again!";
}
}
函数的输出是这样的:
原创 -
"Lorem ipsum
lorem ipsum"
输出 -
“Lorem ipsum ^^^^^ %%% lorem ipsum”
我在SO 上查找了其他答案,但找不到任何可以解决问题的方法...任何帮助将不胜感激! (虽然你可能需要用更基本的术语来解释,但我还是个菜鸟:D)
【问题讨论】:
-
不确定,但尝试设置 preserveWhiteSpace 属性
-
不,没用 :(
标签: php html xml formatting