我正在编写一个从 mysql 导出数据到 csv 文件的脚本。它的工作非常完美。
当我从管理面板输入带撇号的歌曲标题时,如 test'123'
数据像这样保存在数据库中 test\'123\'
.
现在,当我将数据导出到 csv 文件时,标题会像这样使用反斜杠“\”保存:
现在我希望当我将数据从数据库导出到 csv 文件时,反斜杠只会从 csv 文件中删除,而不是从数据库中删除。
这是我导出数据的代码:
$query = "SELECT plist.playlist_id, plist.playlist_name, plist.playlist_shortcode, psong.song_id, psong.list_order,
psong.song_playlist, psong.mp3, psong.ogg, psong.title, psong.buy, psong.buyy, psong.buyyy, psong.price, psong.cover,
psong.artist
FROM " . $pre . "hmp_songs As psong
LEFT JOIN " . $pre . "hmp_playlists As plist
On plist.playlist_name = psong.song_playlist
Where plist.playlist_id IS NOT NULL
And plist.playlist_name IS NOT NULL
And plist.playlist_shortcode IS NOT NULL
And psong.song_id IS NOT NULL
And psong.list_order IS NOT NULL
And psong.song_playlist IS NOT NULL
And psong.mp3 IS NOT NULL
And psong.ogg IS NOT NULL
And psong.title IS NOT NULL
And psong.buy IS NOT NULL
And psong.buyy IS NOT NULL
And psong.buyyy IS NOT NULL
And psong.price IS NOT NULL
And psong.cover IS NOT NULL
And psong.artist IS NOT NULL";
$result = mysqli_query($link,$query) or die("Error executing query: ".mysqli_error());
$row = mysqli_fetch_assoc($result);
$line = "";
$comma = "";
foreach($row as $name => $value) {
$line .= $comma . '"' . str_replace('"', '""', $name) . '"';
$comma = ",";
}
$line .= "\n";
$out = $line;
mysqli_data_seek($result, 0);
while($row = mysqli_fetch_assoc($result)){
$line = "";
$comma = "";
foreach($row as $value) {
$line .= $comma . '"' . str_replace('"', '""', $value) . '"';
$comma = ",";
}
$line .= "\n";
$out .= $line;
}
$csv_file_name = 'HMP_'.date('Ymd_His').'.csv'; # CSV FILE NAME WILL BE table_name_yyyymmdd_hhmmss.csv
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$csv_file_name);
header("Content-Description:File Transfer");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Type: application/octet-stream');
echo __($out,"hmp");
exit;
我不知道该怎么做,因为我不知道。谢谢
最佳答案
PHP 函数 stripslashes
将为您工作。
示例:
<?php
$str = "Is your name O\'reilly?";
echo stripslashes($str);
?>
Output
Is your name O'reilly?
在您的代码中的用法:
foreach($row as $value) {
$line .= $comma . '"' . str_replace('"', '""', stripslashes($value)) . '"';
$comma = ",";
}
关于php - 从 php mysql 中的 csv 文件的标题中删除反斜杠 "\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33751317/