首页 > 解决方案 > 在 Laravel 中生成 CSV 之前删除 html 标签

问题描述

我在我的项目中使用 Laravel 5.8。

我有这个代码:

class ExportEvents extends Model
{
    use scopeActiveTrait;

    protected $quarded = ['id'];
    protected $fillable = ['title', 'title_on_the_list', 'content', 'date_from', 'hour_from', 'date_to', 'hour_to', 'price', 'responsible_person', 'phone_responsible_person', 'www_responsible_person', 'email_responsible_person'];
    public $timestamps = false;
    protected $table = "event_calendars";
}

use Maatwebsite\Excel\Excel;

public function downloadData(string $type = 'csv')
    {
        $data = ExportEvents::get()->toArray();
        $fileName = 'Events '.now();
        return \Excel::create($fileName, function ($excel) use ($data) {
            $excel->sheet('mySheet', function ($sheet) use ($data) {
                $sheet->fromArray($data);
            });
        })->download($type);
    }
}

此代码工作正常,但我必须从我的 csv 文件中的“内容”列中删除 html 标记。

我怎样才能做到这一点?

标签: phplaravellaravel-5

解决方案


在生成 Excel 文档之前,您需要对数据进行预处理。请参阅此示例代码。

foreach($data as $i => $item){
    $content = strip_tags($item['content']);
    $data[$i]['content'] = $content;
}

推荐阅读