我正在研究一个函数,该函数从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!";
}
}
函数的输出类似于:
Original -
lorem ipsum"
输出-
"Lorem ipsum ^^^^^ %%% Lorem ipsum"
我在SO
上查找了其他答案,但找不到任何可以解决问题的方法…任何帮助将非常感激!(虽然你可能需要用更基本的术语来解释,但我仍然是一个新手:D)
以防这对将来的任何人都有帮助:strip_tags()
删除HTML标签以及PHP标签,所以当我使用strip_tags()时,它删除了所有的<br>
's等。
通过在使用strip_tags()
之前插入一个假字符串来代替换行符解决了这个问题,然后使用str_replace()
重新插入它们(这次作为<br>
)。经验教训:首先阅读文档!> & lt;