我正在尝试隐藏用 Php word 格式化的 HTML。
我用summernote创建了一个html表单。 Summernote 允许用户格式化文本。此文本使用 html 标签保存到数据库中。
接下来使用phpWord,我想将捕获的信息输出到word文档中。请看下面的代码:
$rational = DB::table('rationals')->where('qualificationheader_id',$qualId)->value('rational');
$wordTest = new \PhpOffice\PhpWord\PhpWord();
$newSection = $wordTest->addSection();
$newSection->getStyle()->setPageNumberingStart(1);
\PhpOffice\PhpWord\Shared\Html::addHtml($newSection,$rational);
$footer = $newSection->addFooter();
$footer->addText($curriculum->curriculum_code.'-'.$curriculum->curriculum_title);
$objectWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordTest,'Word2007');
try {
$objectWriter->save(storage_path($curriculum->curriculum_code.'-'.$curriculum->curriculum_title.'.docx'));
} catch (Exception $e) {
}
return response()->download(storage_path($curriculum->curriculum_code.'-'.$curriculum->curriculum_title.'.docx'));
<p class="MsoNormal"><span lang="EN-GB" style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><span style="font-family: Arial;">The want for this qualification originated from the energy crisis in
South Africa in 2008 together with the fact that no existing qualifications
currently focuses on energy efficiency as one of the primary solutions. </span><span style="font-family: Arial;">The fact that energy supply remains under
severe pressure demands the development of skills sets that can deliver the
necessary solutions.</span><span style="font-family: Arial;"> </span><o:p></o:p></span></p><p class="MsoNormal"><span lang="EN-GB" style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-family: Arial;">This qualification addresses the need from Industry to acquire credible
and certified professionals with specialised skill sets in the energy
efficiency field. The need for this skill set has been confirmed as a global
requirement in few of the International commitment to the reduction of carbon
ErrorException (E_WARNING) DOMDocument::loadXML(): Namespace prefix o on p is not defined in Entity, line: 1
最佳答案
问题
解析器提示您的文本在元素标签中包含命名空间,更具体地说是标签上的前缀 <o:p> (其中 o: 是前缀)。好像是some kind of formatting for Word .
重现问题
为了重现这个问题,我不得不挖一点,因为不是 PHPWord 抛出异常,而是 DOMDocument PHPWord 正在使用。下面的代码使用 same parsing method PHPWord 正在使用并且应该输出关于代码的所有警告和通知。
# Make sure to display all errors
ini_set("display_errors", "1");
error_reporting(E_ALL);
$html = '<o:p>Foo <o:b>Bar</o:b></o:p>';
# Set up and parse the code
$doc = new DOMDocument();
$doc->loadXML($html); # This is the line that's causing the warning.
# Print it back
echo $doc->saveXML();
DOMXPath with the namespace ,所以 PHPWord可以利用它。不幸的是,DOMXPath isn't public在 API 中,因此不可能。DOMDocument ,我们也可以使用它,并确保我们不需要安装任何(额外的)依赖项。loadXML 解析 HTML ,这是提示格式的函数。在这种方法中可以抑制错误消息,我们在两种解决方案中都必须这样做。这是由 passing an additional parameter 完成的进loadXML和 loadHTML功能。class TagPrefixFixer {
/**
* @desc Removes all prefixes from tags
* @param string $xml The XML code to replace against.
* @return string The XML code with no prefixes in the tags.
*/
public static function Clean(string $xml) {
$doc = new DOMDocument();
/* Load the XML */
$doc->loadXML($xml,
LIBXML_HTML_NOIMPLIED | # Make sure no extra BODY
LIBXML_HTML_NODEFDTD | # or DOCTYPE is created
LIBXML_NOERROR | # Suppress any errors
LIBXML_NOWARNING # or warnings about prefixes.
);
/* Run the code */
self::removeTagPrefixes($doc);
/* Return only the XML */
return $doc->saveXML();
}
private static function removeTagPrefixes(DOMNode $domNode) {
/* Iterate over each child */
foreach ($domNode->childNodes as $node) {
/* Make sure the element is renameable and has children */
if ($node->nodeType === 1) {
/* Iterate recursively over the children.
* This is done before the renaming on purpose.
* If we rename this element, then the children, the element
* would need to be moved a lot more times due to how
* renameNode works. */
if($node->hasChildNodes()) {
self::removeTagPrefixes($node);
}
/* Check if the tag contains a ':' */
if (strpos($node->tagName, ':') !== false) {
print $node->tagName;
/* Get the last part of the tag name */
$parts = explode(':', $node->tagName);
$newTagName = end($parts);
/* Change the name of the tag */
self::renameNode($node, $newTagName);
}
}
}
}
private static function renameNode($node, $newName) {
/* Create a new node with the new name */
$newNode = $node->ownerDocument->createElement($newName);
/* Copy over every attribute from the old node to the new one */
foreach ($node->attributes as $attribute) {
$newNode->setAttribute($attribute->nodeName, $attribute->nodeValue);
}
/* Copy over every child node to the new node */
while ($node->firstChild) {
$newNode->appendChild($node->firstChild);
}
/* Replace the old node with the new one */
$node->parentNode->replaceChild($newNode, $node);
}
}
TagPrefixFixer::Clean功能。$xml = '<o:p>Foo <o:b>Bar</o:b></o:p>';
print TagPrefixFixer::Clean($xml);
<?xml version="1.0"?>
<p>Foo <b>Bar</b></p>
loadHTML而不是 loadXML那个PHPWord is using它将在将 HTML 加载到类中时删除前缀本身。function cleanHTML($html) {
$doc = new DOMDocument();
/* Load the HTML */
$doc->loadHTML($html,
LIBXML_HTML_NOIMPLIED | # Make sure no extra BODY
LIBXML_HTML_NODEFDTD | # or DOCTYPE is created
LIBXML_NOERROR | # Suppress any errors
LIBXML_NOWARNING # or warnings about prefixes.
);
/* Immediately save the HTML and return it. */
return $doc->saveHTML();
}
cleanHTML功能$html = '<o:p>Foo <o:b>Bar</o:b></o:p>';
print cleanHTML($html);
<p>Foo <b>Bar</b></p>
DOMDocument 添加命名空间的方法。解析器而不实际引发原始警告。所以 - 这个解决方案的执行有点麻烦,我不建议使用它,而是构建你自己的。但是你明白了:function addNamespaces($xml) {
$root = '<w:wordDocument
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:o="urn:schemas-microsoft-com:office:office">';
$root .= $xml;
$root .= '</w:wordDocument>';
return $root;
}
addNamespaces功能$xml = '<o:p>Foo <o:b>Bar</o:b></o:p>';
print addNamespaces($xml);
<w:wordDocument
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:o="urn:schemas-microsoft-com:office:office">
<o:p>Foo <o:b>Bar</o:b></o:p>
</w:wordDocument>
addHtml不会引起任何警告。// Show the default reporting except from warnings
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED & ~E_WARNING);
$text_after = preg_replace('/[a-zA-Z]+:([a-zA-Z]+[=>])/', '$1', $text_before);
$text = '<o:p>Foo <o:b>Bar</o:b></o:p>';
$text = preg_replace('/[a-zA-Z]+:([a-zA-Z]+[=>])/', '$1', $text);
echo $text; // Outputs '<p>Foo <b>Bar</b></p>'
关于php - 带有 PhpWord 的隐蔽 HTML:错误 - DOMDocument::loadXML():p 上的命名空间前缀 o 未在实体中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52483285/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'