mk-j / PHP_XLSXWriter

Lightweight XLSX Excel Spreadsheet Writer in PHP
MIT License
1.84k stars 665 forks source link

BUG: Sheetname loses leading/trailing spaces (which is valid) #291

Closed AzzaAzza69 closed 1 year ago

AzzaAzza69 commented 3 years ago

While a sheetname can't start or end with a single quote ('), spaces are allowed, so the sanitize_sheetname function should be:

public static function sanitize_sheetname($sheetname)
{
        static $badchars  = '\\/?*:[]';
//      static $goodchars = '        ';
//      $sheetname = strtr($sheetname, $badchars, $goodchars);
//      $sheetname = substr($sheetname, 0, 31);
//      $sheetname = trim(trim(trim($sheetname),"'"));//trim before and after trimming single quotes
//      return !empty($sheetname) ? $sheetname : 'Sheet'.((rand()%900)+100);

        static $goodchars = '       ';// looks shorter than badchars due to double backslash to indicate single character
        $sheetname = trim(substr(strtr($sheetname, $badchars, $goodchars),0,31),"'");   // sheetname can't start or end with single quotes
        return !empty(trim($sheetname)) ? $sheetname : 'Sheet'.((rand()%900)+100);  // keep leading/trailing spaces
}