2016-11-24 146 views
2

enter image description here删除HTML标签和HTML实体

我怎么能这样当出口删除所有<p><strong>标签在csv文件?下面

是我的代码编写

if($v == "description"){ 


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



       } 
+2

使用'strip_tags()'。 – Barmar

+0

@Barmar我不认为'strip_tags'工作的HTML实体虽然。所以必须完成'htmlspecialchars_decode'和'strip_tags'的组合。 –

回答

1

不清楚如何构成你展示源数据,说$source,也没有哪一种方式,你会导出清理的数据。

假设它是一个独特的大串,你可以仅仅做到这一点:

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

然后你可以使用结果通过类似explode(PHP_EOL, $clean_data)出口。

否则,如果它是一个数组,你可以迭代它的项目,并使用相同TECHNIC依次清除它们:

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

您可以通过重写_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标记分配到缓冲区变量中。

希望它会有所帮助。