如何将preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($s));
添加到$data
。任何人都可以建议一种方法吗?
$date = date('m-d-Y');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=MD-Beaches-".$date.".csv");
header("Pragma: no-cache");
header("Expires: 0");
include('config.php');
$sql ="select county.title,beach.beach_name,beach.description,beach.latitude,beach.longitude,beach.status_id from beach as beach,county as county where beach.county_id=county.id ";
//$data[] = array("County","Beach Name","Description","Latitude","Longitude","RainFall","Temperature","Satus","Notice");
$data[] = array("County","Beach Name","Description","Latitude","Longitude","Satus");
$result = mysql_query($sql);
while( $row = mysql_fetch_assoc( $result)) {
$data[] = $row; // Inside while loop
}
outputCSV($data);
function outputCSV($data) {
$output = fopen("php://output", "w");
foreach ($data as $rowc) {
fputcsv($output, $rowc);
}
fclose($output);
}
答案 0 :(得分:1)
假设您尝试在preg_replace()
的每个元素上调用$data
语句:使用带有回调函数的array_map()
:
$data = array_map(function($elem) {
return preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($elem));
}, $data);
outputCSV($data);