FIX_EXCEL_SANTIZE_HELPER_IMPROVED
This commit is contained in:
parent
321ff9eb8d
commit
1f1f66cf3a
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Helpers;
|
namespace App\Helpers;
|
||||||
|
|
||||||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
||||||
@ -6,17 +7,11 @@ use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
|||||||
class ExcelSanitizeHelper
|
class ExcelSanitizeHelper
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Array of non-printable characters to be removed from strings.
|
* Regex pattern for non-printable characters to be removed from strings.
|
||||||
*
|
*
|
||||||
* You can modify this array to include any characters you want to remove.
|
* This pattern includes control characters, zero-width characters, and Excel-specific codes
|
||||||
*/
|
*/
|
||||||
private static $nonPrintableChars = [
|
private static $nonPrintablePattern = '/[\x00-\x1F\x7F\x{200B}\x{200C}]|_x[0-9A-F]{4}_/u';
|
||||||
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09",
|
|
||||||
"\x0A", "\x0B", "\x0C", "\x0D", "\x0E", "\x0F", "\x10", "\x11", "\x12", "\x13",
|
|
||||||
"\x14", "\x15", "\x16", "\x17", "\x18", "\x19", "\x1A", "\x1B", "\x1C", "\x1D",
|
|
||||||
"\x1E", "\x1F", "\x7F", "_x000D_", "_x000A_", "_x0009_", "_x0008_", "_x0007_",
|
|
||||||
"_x0006_", "_x0005_", "_x0004_", "_x0003_", "_x0002_", "_x0001_","\u200C"
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sanitizes array data by removing non-printable characters and trimming whitespace from strings.
|
* Sanitizes array data by removing non-printable characters and trimming whitespace from strings.
|
||||||
@ -34,12 +29,11 @@ class ExcelSanitizeHelper
|
|||||||
$cleanData[$key] = self::sanitizeArrayData($value); // Recursive call for nested arrays
|
$cleanData[$key] = self::sanitizeArrayData($value); // Recursive call for nested arrays
|
||||||
} elseif (is_string($value)) {
|
} elseif (is_string($value)) {
|
||||||
// Remove non-printable characters and trim whitespace from strings
|
// Remove non-printable characters and trim whitespace from strings
|
||||||
$cleanData[$key] = trim(str_replace(self::$nonPrintableChars, '', $value));
|
$cleanData[$key] = trim(preg_replace(self::$nonPrintablePattern, '', $value));
|
||||||
} else {
|
} else {
|
||||||
$cleanData[$key] = $value; // Keep non-string/non-array data as is
|
$cleanData[$key] = $value; // Keep non-string/non-array data as is
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $cleanData;
|
return $cleanData;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
// Log the error and the problematic data for debugging
|
// Log the error and the problematic data for debugging
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user