Remove BOM () from imported .csv file

后端 未结 6 1651
温柔的废话
温柔的废话 2020-11-27 20:40

I want to delete the BOM from my imported file, but it just doesn\'t seem to work.

I tried to preg_replace(\'/[\\x00-\\x1F\\x80-\\xFF]/\', \'\', $file);

6条回答
  •  佛祖请我去吃肉
    2020-11-27 20:53

    Correct way is to skip BOM if present in file (https://www.php.net/manual/en/function.fgetcsv.php#122696):

    ini_set('auto_detect_line_endings',TRUE);
    $file = fopen($filepath, "r") or die("Error opening file");
    if (fgets($file, 4) !== "\xef\xbb\xbf") //Skip BOM if present
            rewind($file); //Or rewind pointer to start of file
    
    $i = 0;
    while(($line = fgetcsv($file, 1000, ";")) !== FALSE) {
        ...
    }
    

提交回复
热议问题