1. <?php
  2. /**
  3. * Formats a text file for manual execution.
  4. * Preserves original content and adds basic formatting.
  5. *
  6. * @param string $filename The path to the text file.
  7. * @return string|false The formatted content as a string, or false on error.
  8. */
  9. function formatTextFile($filename) {
  10. if (!file_exists($filename)) {
  11. error_log("File not found: " . $filename);
  12. return false;
  13. }
  14. $fileContent = file_get_contents($filename);
  15. if ($fileContent === false) {
  16. error_log("Failed to read file: " . $filename);
  17. return false;
  18. }
  19. // Add a header
  20. $formattedContent = "<?php\n\n/**\n * This code was generated from the file: " . $filename . "\n * Please review and modify as needed.\n */\n\n";
  21. $formattedContent .= $fileContent;
  22. return $formattedContent;
  23. }
  24. // Example usage (uncomment to test)
  25. /*
  26. $formattedCode = formatTextFile("my_script.txt");
  27. if ($formattedCode !== false) {
  28. echo $formattedCode;
  29. // Optionally, write to a new file:
  30. // file_put_contents("formatted_script.php", $formattedCode);
  31. } else {
  32. echo "Formatting failed.";
  33. }
  34. */
  35. ?>

Add your comment