Php 在HTML中显示XML-如何保留换行符?
我正在开发一个函数,该函数从Microsoft WordPhp 在HTML中显示XML-如何保留换行符?,php,html,xml,formatting,Php,Html,Xml,Formatting,我正在开发一个函数,该函数从Microsoft Word.docx文件中获取内容,并将其显示在网页中。文本渲染很好,但我似乎无法让它显示换行符 我的函数代码如下: function readDocx($filePath) { $zip = new ZipArchive; //Create new ZIP archive $dataFile = "word/document.xml"; //Open received archive file if (true === $zip->open
.docx
文件中获取内容,并将其显示在网页中。文本渲染很好,但我似乎无法让它显示换行符
我的函数代码如下:
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!";
}
}
函数readDocx($filePath){
$zip=新的ZipArchive;
//创建新的ZIP存档
$dataFile=“word/document.xml”;
//打开收到的存档文件
如果(true===$zip->open($filePath)){
//如果打开成功,请在存档中搜索数据文件
if(($index=$zip->locateName($dataFile))!==false){
//如果找到,将其读取到字符串
$data=$zip->getFromIndex($index);
//从字符串加载XML。跳过错误和警告
$xml=newdomdocument();
$xml->loadXML($data,LIBXML|NOENT | LIBXML|XINCLUDE | LIBXML|NOERROR | LIBXML|NOWARNING);
$xmldata=$xml->saveXML();
$xmldata=str_replace(“,”,“^^^^^”。
”。
“\n”。
“”,$xmldata);
回波带标签($xmldata);
}
$zip->close();
}
//如果失败,则返回一个空字符串
否则{
echo“打开文件时出错-请重试!”;
}
}
函数的输出类似于: 原创- “同侧眼界 “同侧眼” 输出- “Lorem ipsum^^^^%%%Lorem ipsum”
我在
上查找了其他答案,所以
没有找到任何有助于解决问题的答案。。。任何帮助都将不胜感激!(虽然你可能需要用更基本的术语来解释,但我还是个新手:D)如果这对将来的任何人都有帮助:strip\u tags()
删除HTML标记和PHP标记,所以当我使用strip\u tags()时,它也删除了所有的
等等
通过在使用
strip\u tags()
之前插入一个伪字符串来代替换行符,然后使用str\u replace()
重新插入它们(这次作为
)来解决此问题。经验教训:先阅读文档!>不确定,但尝试设置preserveWhiteSpace propertynope,不起作用:(