简体   繁体   English

去掉 <br /> PHP csv导出中的标签

[英]Remove <br /> tags from PHP csv export

I am using the following code to export data into a CSV file using PHP. 我正在使用以下代码使用PHP将数据导出到CSV文件中。 The problem is that one of the fields has line breaks in it the CSV file shows multiple <br /> tags. 问题在于,其中一个字段中有换行符,CSV文件显示了多个<br />标记。 Is there an easy way to remove these from the output file? 有一种简单的方法可以从输出文件中删除它们吗?

<?php


$query = "SELECT * FROM table";

$result = $conn->query($query);

header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=output.csv');

$row = mysqli_fetch_assoc($result);
if ($row) {
    echocsv(array_keys($row));
}

while ($row) {
    echocsv($row);
    $row = mysqli_fetch_assoc($result);
}

function echocsv($fields)
{
    $separator = '';
    foreach ($fields as $field) {
        if (preg_match('/\\r|\\n|,|"/', $field)) {
            $field = '"' . str_replace('"', '""', $field) . '"';
        }
        echo $separator . $field;
        $separator = ',';
    }
    echo "\r\n";
}
?>

Thanks, 谢谢,

John 约翰

Have you tried str_replace? 您尝试过str_replace吗? -> http://php.net/manual/es/function.str-replace.php -> http://php.net/manual/es/function.str-replace.php

$html = 'Lorep Ipsum<br/>Dolor Sit Amet';
echo '<p>' . $html . '</p>';

$html= str_replace('<br/>', "", $html);
echo '<p>' . $html . '</p>';

Output: 输出:

Lorep Ipsum
Dolor Sit Amet

Lorep IpsumDolor Sit Amet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM