1. <?php
  2. /**
  3. * Cleans data from a file, removing potentially harmful characters and formatting.
  4. *
  5. * @param string $filePath The path to the file to clean.
  6. * @return string|false The cleaned data as a string, or false on error.
  7. */
  8. function cleanFileData(string $filePath): string|false
  9. {
  10. if (!file_exists($filePath)) {
  11. error_log("File not found: $filePath");
  12. return false;
  13. }
  14. $fileContent = file_get_contents($filePath);
  15. if ($fileContent === false) {
  16. error_log("Failed to read file: $filePath");
  17. return false;
  18. }
  19. // Remove HTML tags
  20. $fileContent = strip_tags($fileContent);
  21. // Remove Javascript
  22. $fileContent = preg_replace('/<script.*?>.*?<\/script>/s', '', $fileContent);
  23. // Remove CSS
  24. $fileContent = preg_replace('/<style.*?>.*?<\/style>/s', '', $fileContent);
  25. // Remove comments
  26. $fileContent = preg_replace('/<!--.*?-->/', '', $fileContent);
  27. // Remove potentially harmful characters (e.g., backticks, single quotes)
  28. $fileContent = str_replace(["`", "'"], "", $fileContent);
  29. // Remove extra whitespace
  30. $fileContent = trim(preg_replace('/\s+/', ' ', $fileContent));
  31. // Optionally, handle specific characters if needed. Example: Remove control characters.
  32. $fileContent = preg_replace('/[\x00-\x1F\x7F]/', '', $fileContent);
  33. return $fileContent;
  34. }
  35. // Example usage:
  36. //$cleanedData = cleanFileData('path/to/your/file.txt');
  37. //if ($cleanedData !== false) {
  38. // echo $cleanedData;
  39. //} else {
  40. // echo "Error cleaning file.";
  41. //}
  42. ?>

Add your comment