删除反斜杠" \"来自php mysql

时间:2015-11-17 07:26:24

标签: php mysql wordpress csv

我正在编写一个从csv文件中的mysql导出数据的脚本。它的工作非常完美。

当我从管理面板(例如test'123')输入带有撇号的歌曲标题时,数据保存在数据库中,如test\'123\'

enter image description here

现在,当我将数据导出到csv文件中时,标题保存为反斜杠' \'像这样:

enter image description here

现在我希望当我将数据从数据库导出到csv文件时,反斜杠只会从csv文件中删除,而不是从数据库中删除。

这是我的expol数据代码:

$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;

我怎么能这样做,因为我不知道。感谢

1 个答案:

答案 0 :(得分:6)

PHP函数stripslashes将适合您。

示例:

<?php
$str = "Is your name O\'reilly?";
echo stripslashes($str);
?>
  

输出

     

你的名字是O&#39;赖利?

代码中的用法:

foreach($row as $value) {
    $line .= $comma . '"' . str_replace('"', '""', stripslashes($value)) . '"';
    $comma = ",";
}