从HTML中删除Span标记

时间:2014-04-22 11:55:50

标签: php html dom

以下是一个woking代码,它将docx转换为HTML,并将删除除了src之外的所有HTML属性,来自img标签和href来自标签..现在我想删除所有span标签......如何做到

<?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中的所有span标记,然后将其保存在第60行的recode文件夹中,如下所示

file_put_contents('Recode/' . $flname . '.html', base64_decode($this->data));

0 个答案:

没有答案