Skip to content Skip to sidebar Skip to footer

Php Dom Implementation

I have a convertor which will convert doc and docx to html for that convertor the class file is as follows

Solution 1:

Try this solution:

<?phpclassdocxhtml{
    /** @var string */private$tag;
    /** @var string */private$attribute;

    public$connectname;
    public$connectpass;

    publicfunction__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));
    }

    publicfunctionstripAttributes(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 ($elementsas$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());
    }

    publicfunctioncheckAttrExceptions(array$exceptions)
    {
        foreach ($exceptionsas$tag => $attributes) {
            if (empty($attributes) || !is_array($attributes)) {
                die('Attributes not set!');
            }

            foreach ($attributesas$attribute) {
                if ($tag === $this->tag && $attribute === $this->attribute) {
                    returntrue;
                }
            }
        }

        returnfalse;
    }
}

If you need to add more attribute exceptions, just edit $exceptions array. For example, if you don't want strip title attribute in all "a" tags modify exceptions:

$exceptions = array(
    'a'   => array('href', 'title'),
    'img' => array('src')
);

Post a Comment for "Php Dom Implementation"