【发布时间】:2014-04-22 04:14:30
【问题描述】:
我正在使用以下代码将 doc 和 docx 转换为剥离的 html
<?php
class docxhtml
{
/** @var string */
private $tag;
/** @var string */
private $attribute;
public $connectname;
public $connectpass;
public function __construct($format_res, $flname)
{
require_once('config.php');
// Turn up error reporting
error_reporting(E_ALL | E_STRICT);
// Turn off WSDL caching
ini_set('soap.wsdl_cache_enabled', 0);
// Define credentials for LD
define ('USERNAME', $this->connectname);
define ('PASSWORD', $this->connectpass);
// SOAP WSDL endpoint
define ('ENDPOINT', 'https://api.livedocx.com/2.1/mailmerge.asmx?wsdl');
// Define timezone
date_default_timezone_set('Europe/Berlin');
// Instantiate SOAP object and log into LiveDocx
$this->soap = new SoapClient(ENDPOINT);
$this->soap->LogIn(
array('username' => USERNAME, 'password' => PASSWORD)
);
// Upload template
$this->data = file_get_contents('Original/' . $format_res);
$this->soap->SetLocalTemplate(
array('template' => base64_encode($this->data), 'format' => 'docx')
);
$this->result = $this->soap->RetrieveDocument(
array('format' => 'html')
);
$this->data = $this->result->RetrieveDocumentResult;
$exceptions = array(
'a' => array('href'),
'img' => array('src')
);
$this->stripAttributes($exceptions);
file_put_contents('Recode/' . $flname . '.html', base64_decode($this->data));
}
public function stripAttributes(array $exceptions)
{
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
$dom->formatOutput = true;
$dom->loadHTML(base64_decode($this->data));
$xpath = new DOMXPath($dom);
if (false === ($elements = $xpath->query("//*"))) die('Xpath error!');
/** @var $element DOMElement */
foreach ($elements as $element) {
for ($i = $element->attributes->length; --$i >= 0;) {
$this->tag = $element->nodeName;
$this->attribute = $element->attributes->item($i)->nodeName;
if ($this->checkAttrExceptions($exceptions)) continue;
$element->removeAttribute($this->attribute);
}
}
$this->data = base64_encode($dom->saveHTML());
}
public function checkAttrExceptions(array $exceptions)
{
foreach ($exceptions as $tag => $attributes) {
if (empty($attributes) || !is_array($attributes)) {
die('Attributes not set!');
}
foreach ($attributes as $attribute) {
if ($tag === $this->tag && $attribute === $this->attribute) {
return true;
}
}
}
return false;
}
}
现在我想要的是将规则和过滤器添加到生成的 html 输出中,如下所示
1) 所有居中的标题应该是Div 标签,现在是P 标签
Sample : http://oi58.tinypic.com/2b6v0k.jpg
2) 此外,居中标题下的作者姓名应位于单独的 Div 标记中,该标记现在也在 P 标记中(“就像上面示例图片中的单词 - Francis Bacon”)
3) 其余所有标题都应位于P 带有属性class ="h1 or h2 or..h" 的标签中,其中应使用字体大小检测h 值
4) 如果<li> 标签内只有一行,那么它应该是<li> content </li> 目前它是<li><p>content </p></li>
5) 假设如果<li> 标签包含多行,则应该有p 标签
6) 删除所有Span 标签
请为我提供建议和指导...
编辑:要转换的 Html 文件的链接http://www64.zippyshare.com/v/80261796/file.html
【问题讨论】:
-
您的前两个“规则”对我来说没有意义 - 没有足够的信息。如果您共享了一个您也想要转换的示例 HTML 文档,将会很有帮助。
-
@jonnu 添加了 html 文件链接,正如我在原始帖子底部部分中编辑的那样。参考:www64.zippyshare.com/v/80261796/file.html
-
我只是在检查 HTML 源代码。它是没有语义信息的标签汤——我怀疑在这个文档上执行步骤 1、2、3 几乎是不可能的——不是以可重复用于另一个文档的方式。例如,您能否用一句话准确地写出您希望代码如何确定哪个标签包含标题,以及哪个标签包含作者姓名?但是,使用 PHP 的
DOM扩展名肯定可以实现 4、5 和 6。 -
@jonnu 你能告诉我第 6 点,我推迟了休息...只是说应该在我上面说的代码中添加什么代码以使用 dom 删除所有跨度标签。非常感谢您的回复伴侣
-
当然,没问题。已发布答案。