$value) { if (is_array($value)) { $cleanData[$key] = self::sanitizeArrayData($value); // Recursive call for nested arrays } elseif (is_string($value)) { // Remove non-printable characters and trim whitespace from strings $value = str_replace("\u00a0", " ", $value); $cleanData[$key] = trim(preg_replace(self::$nonPrintablePattern, '', $value)); } else { $cleanData[$key] = $value; // Keep non-string/non-array data as is } } return $cleanData; } catch (\Exception $e) { // Log the error and the problematic data for debugging log_message('error', 'Error sanitizing array data: ' . $e->getMessage()); log_message('error', 'Problematic data: ' . json_encode($data)); return $data; // Return the original data in case of error } } }