Ch Hong
Ch Hong

Reputation: 378

Remove HTML tag and html entities in csv file

enter image description here

How can i remove this all <p><strong> tag in csv file when export?

below is my code write

 if($v == "description"){


               $q[$v] = preg_replace("/&#?[a-z0-9]+;/i","",$q[$v]); 



                }

Upvotes: 2

Views: 4289

Answers (3)

Ahsan Horani
Ahsan Horani

Reputation: 329

strip_tags() will be used to strip the HTML tags on export. Just define following function inside your controller :

protected function _prepareDownloadResponse(
        $fileName,
        $content,
        $contentType = 'application/octet-stream',
        $contentLength = null)
    {
        $session = Mage::getSingleton('admin/session');
        if ($session->isFirstPageAfterLogin()) {
            $this->_redirect($session->getUser()->getStartupPageUrl());
            return $this;
        }

        $isFile = false;
        $file   = null;
        if (is_array($content)) {
            if (!isset($content['type']) || !isset($content['value'])) {
                return $this;
            }
            if ($content['type'] == 'filename') {
                clearstatcache();
                $isFile         = true;
                $file           = $content['value'];
                $contentLength  = filesize($file);
            }
        }

        $this->getResponse()
            ->setHttpResponseCode(200)
            ->setHeader('Pragma', 'public', true)
            ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
            ->setHeader('Content-type', $contentType, true)
            ->setHeader('Content-Length', is_null($contentLength) ? strlen($content) : $contentLength, true)
            ->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"', true)
            ->setHeader('Last-Modified', date('r'), true);

        if (!is_null($content)) {
            if ($isFile) {
                $this->getResponse()->clearBody();
                $this->getResponse()->sendHeaders();

                $ioAdapter = new Varien_Io_File();
                $ioAdapter->open(array('path' => $ioAdapter->dirname($file)));
                $ioAdapter->streamOpen($file, 'r');

                //Strip HTML here
                while ($buffer = strip_tags($ioAdapter->streamRead())) {
                    print $buffer;
                }
                $ioAdapter->streamClose();
                if (!empty($content['rm'])) {
                    $ioAdapter->rm($file);
                }

                exit(0);
            } else {
                $this->getResponse()->setBody($content);
            }
        }
        return $this;
    }

Upvotes: 0

Randhir Yadav
Randhir Yadav

Reputation: 1459

You can escape HTML tags by overriding the _prepareDownloadResponse() controller function. This function declares headers and content file in response for file download.

To do so, you have to first rewrite/override the Mage_Adminhtml_Sales_OrderController controller class same as below.

app/code/local/Namespace/Module/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>

    ...

    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Namespace_Module before="Mage_Adminhtml">Namespace_Module_Adminhtml</Namespace_Module>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

    ...

</config>

app/code/local/Namespace/Module/controllers/Adminhtml/Sales/OrderController.php

<?php

    require_once "Mage/Adminhtml/controllers/Sales/OrderController.php";

    class Namespace_Module_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController
    {
        /**
         * Declare headers and content file in response for file download
         *
         * @param string $fileName
         * @param string|array $content set to null to avoid starting output, $contentLength should be set explicitly in
         *                              that case
         * @param string $contentType
         * @param int $contentLength    explicit content length, if strlen($content) isn't applicable
         * @return Mage_Core_Controller_Varien_Action
         */
        protected function _prepareDownloadResponse(
            $fileName,
            $content,
            $contentType = 'application/octet-stream',
            $contentLength = null)
        {

            ...

            if (!is_null($content)) {
                if ($isFile) {

                    ...

                    // strip tags from data
                    while ($buffer = strip_tags($ioAdapter->streamRead())) {
                        print $buffer;
                    }

                    ...

                } else {
                    $this->getResponse()->setBody($content);
                }
            }
            return $this;
        }
    }

as you can see, strip_tags is being used to strip the HTML tags before assigning into the buffer variable.

Hope it will help.

Upvotes: 1

cFreed
cFreed

Reputation: 4474

Not clear how is structured the source data you're showing, say $source, nor which way you'll export the cleaned data.

Assuming it's a unique big string, you can merely do this:

$clean_data = strip_tags(html_entity_decode($source));

Then you can use the result through something like explode(PHP_EOL, $clean_data) to export.

Otherwise if it's an array you can iterate its items and use the same technic to successively clean each of them:

foreach ($source as $line) {
    $clean_line = strip_tags(html_entity_decode($line));
    ... export the clean line
}

Upvotes: 1

Related Questions