当我从数据库中导出数据时,我有一个名为description的字段(里面可以有一个<br /><img
等html标签....);
当我在读取此字段时导出时,我的cvs停止正常工作。
我看不到函数内部发生了什么。
看来,当它读取类似<br />
的内容时,就会为<br
创建2 cel,例如为/>
创建2 cel。
我插入图片来购物。
谢谢。
$Qproducts = $this_Db->prepare('select p.*,pd.*
from :table_products p,:table_products_description pd
where p.products_id = pd.products_id
and language_id = :language_id
');
$Qproducts->bindInt('language_id',$language_id);
$Qproducts->execute();
while ($Qproducts->fetch()) {
--example
$csv[] = [
'products_id' => $Qproducts->valueInt('products_id'),'products_description' => $Qproducts->value('products_description'),];
}
ob_clean();
if ($output == 'screen') {
header('Content-Type: text/plain; charset=utf-8');
} else {
header('Content-Type: application/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=products-' . $language_id . '.csv');
}
echo ImportExportAdmin::csvEncode($csv,$delimiter,$enclosure,$escapechar,null,"\r\n");
public static function csvEncode(array $array,string $delimiter=',',$enclosure='"',$escape='"',$charset='utf-8',string $eol="\r\n") {
$output = '';
// Collect columns
$columns = [];
foreach ($array as $row) {
foreach (array_keys($row) as $column) {
if (!in_array($column,$columns)) $columns[] = $column;
}
}
// Collect rows and order by column order
foreach (array_keys($array) as $row) {
$line = [];
foreach ($columns as $column) {
$line[$column] = isset($array[$row][$column]) ? $array[$row][$column] : '';
}
$array[$row] = $line;
}
// Prepend column header
array_unshift($array,array_combine($columns,$columns));
// Build output
foreach ($array as $row) {
foreach (array_keys($row) as $column) {
if (strpbrk($row[$column],$delimiter . $enclosure . $escape."\r\n") !== false) {
$row[$column] = $enclosure . str_replace($enclosure,$escape . $enclosure,$row[$column]) . $enclosure;
}
}
$output .= implode($delimiter,$row) . $eol; // Don't use fputcsv as EOL and escape char can not be customized
}
return preg_replace('#(\r\n|\r|\n)#',$eol,$output);
}
说明示例
<img alt="" src="/test/shop/sources/images/products/70_PKAh4DX3Hy_logo.png" style="width: 70px; height: 71px;" />The 30-inch Apple Cinema HD Display delivers an amazing 2560 x 1600 pixel resolution. Designed specifically for the creative professional,this display provides more space for easier access to all the tools and palettes needed to edit,format and composite your work. Combine this display with a Mac Pro,MacBook Pro,or PowerMac G5 and there's no limit to what you can achieve.<br />
<br />
Resolutions<br />
2560 x 1600 pixels (optimum resolution)<br />
2048 x 1280<br />
1920 x 1200<br />
1280 x 800<br />
1024 x 640<br />