删除csv文件中的HTML标记和html实体

时间:2016-11-24 03:27:15

标签: php

enter image description here

如何在导出时删除csv文件中的所有<p><strong>标记?

下面是我的代码编写

 if($v == "description"){


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



                }

3 个答案:

答案 0 :(得分:1)

不清楚您显示的源数据的结构如何,比如$source,以及您导出清理数据的方式。

假设它是一个独特的大字符串,你只能这样做:

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

然后,您可以通过explode(PHP_EOL, $clean_data)之类的内容使用结果导出。

否则,如果它是一个数组,你可以迭代它的项目并使用相同的技术来连续清理每个项目:

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

答案 1 :(得分:1)

您可以通过覆盖_prepareDownloadResponse()控制器功能来转义HTML标记。此函数声明头文件和内容文件以响应文件下载。

为此,您必须首先重写/覆盖Mage_Adminhtml_Sales_OrderController控制器类,如下所示。

  

应用程序/代码/本地/命名空间/模块的/ 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>
  

应用程序/代码/本地/命名空间/模块/控制器/ Adminhtml /销售/ 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;
        }
    }

如您所见,strip_tags用于在分配到缓冲区变量之前去除HTML标记。

希望它会有所帮助。

答案 2 :(得分:0)

strip_tags()将用于在导出时剥离HTML标签。只需在您的控制器中定义以下功能:

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