protectFormulaInjection($stringValue); } /** * Prevents Spreadsheet Formula Injection (CSV Injection) attacks. * * @param string $value * @return string Escaped cell value. */ private function protectFormulaInjection(string $value): string { if ($value === '') { return ''; } $formulaChars = ['=', '+', '-', '@', "\t", "\r"]; $firstChar = substr($value, 0, 1); if (in_array($firstChar, $formulaChars, true)) { // Prepend a single quote to instruct Google Sheets to treat this cell value strictly as raw text. return "'" . $value; } return $value; } }